取消或解除android alertdialog

时间:2013-05-17 11:18:19

标签: android web-services android-asynctask

我正在使用以下函数在我的android视图中创建/显示加载

public void showDialogue(String Message, String Title){
    builder = new AlertDialog.Builder(this);
    progress = new ProgressBar(this);
    builder.setMessage(Message);
    builder.setView(progress);
    builder.create().show();

}

我将此函数称为asyn tasklike

private class SetParm extends AsyncTask<String, Integer, String> {

        Integer myid;
        Integer myFlag;
        Integer removeId;
        @Override
        protected String doInBackground(String... sUrl) {
            try {

                SharedPreferences mPrefs = getSharedPreferences("prefs",0);    
                String restoredText = mPrefs.getString("access_token", ""); 
                String path = "http://www.sitename.com/app/setFlag.php";

                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout

                HttpResponse response;
                JSONObject json = new JSONObject();
                try {
                    HttpPost post = new HttpPost(path);
                    json.put("access_token", restoredText);
                    json.put("id", myid);
                    json.put("flag", myFlag);

                    Log.i("jason Object", json.toString());
                    post.setHeader("json", json.toString());
                    StringEntity se = new StringEntity(json.toString());
                    se.setContentEncoding((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    post.setEntity(se);
                    response = client.execute(post);
                    /* Checking response */
                    if (response != null) {
                        InputStream in = response.getEntity().getContent(); 

                        String a = convertStreamToString(in);

                        JSONObject jsono = stringToJsonobj(a);
                        Log.v("TAGG",a);
                        String passedStringValue = jsono.getString("result");


                        if(passedStringValue.equals("1")){
                            flags=1;
                            //Log.v("TAGG", "Success");
                            SharedPreferences mPrefss = getSharedPreferences("prefs", 0);    
                            SharedPreferences.Editor editor = mPrefss.edit();    
                            editor.putString("access_token", jsono.getString("access_token"));  
                            editor.commit();
                        }
                        else {
                            flags=0;
                            //Log.v("TAGG", "Failed !");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


            } catch (Exception e) {
            }
            return null;
        }


        @Override
        protected void onPreExecute() {
            showDialogue("Regestring Your Devide... Please wait.", "Regestring Devide");

            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
        }



        @Override
        protected void onPostExecute(String result) {
            builder.cancel();
            if(flags.equals(1)){
                TableLayout mTable = (TableLayout)findViewById(R.id.tableLayout_1);
                mTable.removeView(findViewById(4500+removeId));
                mTable.removeView(findViewById(6500+removeId));

                int count = mTable.getChildCount();
                if(count<=0){
                    lists="";
                    total="0";
                    LogIN loginUsers1 = new LogIN();
                    loginUsers1.execute("");

                }

            }
            else {
                TextView text = (TextView) findViewById(R.id.status_msg);
                text.setText("Error while processing requests. Please try again.");
            }
            super.onPostExecute(result);
        }

    }

从onPreExecute()函数调用警报对话框。

现在,我需要在完成webservice请求后删除加载

所以我在builder.cancel();中写了onPostExecute,但它无效

有什么想法吗? 提前致谢

6 个答案:

答案 0 :(得分:3)

您可以使用dialog,而不是正常progress dialog,如下所示:

private ProgressDialog progressDialog;

protected void onPreExecute() {
            super.onPreExecute();
            progressDialog= new ProgressDialog(MainActivity.this);
            progressDialog.setMessage("Loading ....");
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(true);
            progressDialog.show();
        }

 protected void onPostExecute(String result) {
progressDialog.dismiss();
}

答案 1 :(得分:3)

  

自定义进度栏.....

         //Pre Execute method

            @Override
            protected void onPreExecute()
            {
                super.onPreExecute();
                ProgressDialog pDialog = new ProgressDialog(Activity.this);

                pDialog= ProgressDialog.show(.this, "","Loading.    Please wait...", true);
                pDialog.setContentView(R.layout.progressbar);

                pDialog.setCancelable(false);

            }

    ////////////////////////////////////////////////////////////////////////////////////////

        Progress bar xml layout
        that is progressbar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="150dip"
        android:layout_height="150dip"
        android:background="@drawable/progressbar_shap"
        android:layout_gravity="center">

      <TextView  android:id="@+id/textview"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:text="Please Wait..."
       android:textStyle="bold"
      />
      <ProgressBar 
       android:indeterminateDrawable="@drawable/my_progress_indetermine"
       android:layout_height="60dp" 
       android:layout_width="60dp"
       android:layout_centerInParent="true"
       ></ProgressBar>
    </RelativeLayout>


  ////////////////////////////////////////////////////////////////////////
  After that 
  my_progressbar_indertimine.xml

    <?xml version="1.0" encoding="utf-8"?>
    <animated-rotate 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:drawable="@drawable/please_wait"
     android:pivotX="50%"
     android:pivotY="50%" />





//Post execute method , which will dismiss progress bar.


@Override
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all albums
            pDialog.dismiss();
}

“please_wait”进度条图像,它将显示白色,当您使用下面的链接时   ,但您可以保存它,右键单击浏览器页面并使用“另存为”

  http://i.stack.imgur.com/hnY8r.png

答案 2 :(得分:2)

尝试这样

    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Activity.this);
        pDialog.setMessage("Loading Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }   

在onPostExecute中这样做

     protected void onPostExecute(String result) {
        super.onPostExecute(toString());
        // dismiss the dialog after loading
        pDialog.dismiss();          
            }

答案 3 :(得分:1)

您可以使用Dialog代替AlertDialog,并在dialog.dismiss()

中致电onPostExecute()

编辑:请不要将此答案视为解决方案。因为我认为ramesh正在使用AlertDialog.Builder,并且回答错误。和Dialog一样,我们有更多的自定义选项,我建议它。

答案 4 :(得分:0)

在你的onPostExecute中试试这个......

    @Override
    protected void onPostExecute(String result) {
        try{
        builder.dismiss();
         }
      catch(Exception e){

            }
        if(flags.equals(1)){
            TableLayout mTable = (TableLayout)findViewById(R.id.tableLayout_1);
            mTable.removeView(findViewById(4500+removeId));
            mTable.removeView(findViewById(6500+removeId));

            int count = mTable.getChildCount();
            if(count<=0){
                lists="";
                total="0";
                LogIN loginUsers1 = new LogIN();
                loginUsers1.execute("");

            }

        }
        else {
            TextView text = (TextView) findViewById(R.id.status_msg);
            text.setText("Error while processing requests. Please try again.");
        }

    }

答案 5 :(得分:0)

尝试使用此代码进行异步任务和进度对话框

    public class MyTask extends AsyncTask<Void, Integer, Void> {

    public MyTask(Activity activity, int id) {
        this.activity = activity;


        context = activity;

    }

    /** progress dialog to show user that the backup is processing. */
    private ProgressDialog progressDialog;
    /** application context. */
    private Activity activity;
    private Context context;


    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        progressDialog = ProgressDialog.show(context, "", "Loading...");



    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

                    return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
                    progressDialog.dismiss();

             }

}