Android 如何显示进度对话框甚至触摸屏幕

How to show progress dialog even touch the screen in Android

我正在使用进度对话框检索 Flickr 照片,我的要求是 1) 即使我触摸屏幕也显示进度对话框,并且 2)如果我点击后退按钮异步任务应该取消并返回到上一页

这里我尝试了第二个要求,它即将到来,但我没有得到第一个要求。你能帮我解决这个问题吗

这是我的 onPreExecute 代码

 DownloadTask asyncClassObject;
       private class DownloadTask extends AsyncTask<String, Integer, String>{


          protected void onPreExecute() {
                        super.onPreExecute();
                     **asyncClassObject = new DownloadTask();**
                        // Showing progress dialog      
                        pDialog =  ProgressDialog.show(Flickeralbumlist.this,"Flickr Album","please wait...",true,true,new DialogInterface.OnCancelListener() {

                            @Override
                            public void onCancel(DialogInterface dialog) {


                                DownloadTask.this.cancel(true);
                                finish();
                            }
                        }); 
                        pDialog.setCanceledOnTouchOutside(false);
                        pDialog.setCancelable(false);
                        **asyncClassObject.execute();**
            }
    }

          @Override
                public void onBackPressed() {
                                 // cancel AsyncTask
                            if (asyncClassObject != null)
                                asyncClassObject.cancel(true);
                             // dismiss ProgressDialog
                            if(pDialog !=null)
                               if(pDialog.isShowing())
                                 pDialog.dismiss();

                             super.onBackPressed();
                }

虽然我没有得到正确的输出,但我在这里使用了这一行"pDialog.setCanceledOnTouchOutside(false);",你能帮我解决这个问题吗?

1)Display progress dialog even I touch the screen

创建ProgressDialog后添加setCancelable

pDialog.setCanceledOnTouchOutside(false);
pDialog.setCancelable(false);

2)If I click back button the asyn task should cancel and go back to previous page

用真值调用 AsyncTask.cancel

@Override
public void onBackPressed()
{
    // cancel AsyncTask
    if (asyncClassObject != null)
        asyncClassObject.cancel(true);

    // dismiss ProgressDialog
    if(pDialog !=null)
       if(pDialog.isShowin())
         pDialog.dismiss()
    super.onBackPressed();
}

使用以下代码获取点击事件.....

     View view = (View)findViewById(R.id.mainLayout);

     view.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            System.out.println("Clicked!!!"); /// Write your Code for Progress Here....
            return true;
        }
    });