在android中等待进度条

Lin*_*vel 1 android progressdialog android-progressbar progress-bar

在我的Android应用程序中,我将一些数据从服务器数据库复制到本地数据库.代码发布在下面.可能需要15分钟.对于较慢的连接,可能会超出.所以我想显示一个等待进度条,它应显示"请等待",直到任务完成.我引用了很多东西,但我无法理解如何使用以下代码中的等待进度条.

refresh.setOnClickListener(new OnClickListener(){

public void onClick(View v) {
    mySQLiteAdapter.openToWrite();
    mySQLiteAdapter.deleteAll();
    radapter.openToWrite();
    radapter.deleteAll();
    uadapter.openToWrite();
    uadapter.deleteAll();
            try
                {
                     HttpClient httpclient = new DefaultHttpClient();
                     HttpPost httppost = new HttpPost("http://xxxxxxxxxxxxxxxxxxxxx");
                     HttpResponse response = httpclient.execute(httppost);
                     HttpEntity entity = response.getEntity();
                      is = entity.getContent();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(
                            is, "iso-8859-1"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();
                    json = sb.toString();
                } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
                }

                try {
                    jObj = new JSONObject(json);
                    contacts = jObj.getJSONArray("get");
                    for(int i = 0; i < contacts.length(); i++){
                    JSONObject c = contacts.getJSONObject(i);
                        if(!c.isNull("rname"))// || c.getString("rcode").equals(null))
                        {
                            rname = c.getString("rname");
                            rcode = c.getString("rcode");
                            radapter.insert(rcode, rname);
                        }
                        else
                        {
                            rname = "";
                            rcode = c.getString("rcode");
                            radapter.insert(rcode, rname);
                        }
                    }
                    Toast.makeText(getBaseContext(), " Ryot BackUp completed", Toast.LENGTH_SHORT).show();

                } catch (JSONException e) {
                    Toast.makeText(getBaseContext(), "Error"+e.toString(), Toast.LENGTH_LONG).show();
                }
                adapter.deleteAll();
                oadapter.deleteAll();
                e2.setText("Back Up completed");
            }

       });
Run Code Online (Sandbox Code Playgroud)

请有人帮帮我.提前致谢

Ava*_*i Y 6

使用Progress Dialog具有文字显示" 请稍候... ",并在执行你的代码thread如下:

      final ProgressDialog progressDialog = ProgressDialog.show(this, "", "Please wait...");
      new Thread() {
          public void run() {
                try{
                      //your code here.....
                   } 
                catch (Exception e) {
                    Log.e("tag", e.getMessage());
                   }
                // dismiss the progress dialog
                       progressDialog.dismiss();
                   }
                }.start();
Run Code Online (Sandbox Code Playgroud)