当我在android中更改活动时,进度条消失

时间:2013-09-02 12:50:12

标签: android android-asynctask android-progressbar

我编写了使用带有进度条的异步任务下载我的视频文件的代码,它工作正常..但如果我改变我的活动或将我的应用程序放在后台,当我回到我的活动(或应用程序)而不是进度条已经消失,但下载仍然在后台进行,但我没有得到进展吧...

我把进度条显示代码放在异步任务的Preexecute方法和assectask的postexecute中的dissmiss方法中。

我在google上搜索存储状态但在我的情况下它将无法正常工作..

这是我的代码

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

     PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);

     partialWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Loneworker - PARTIAL WAKE LOCK");


    setContentView(R.layout.restore);


    final Button restore =(Button)findViewById(R.id.restore);
    restore.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            new DownloadFileFromURL().execute(listOfStrings); // pass the arraylist which contains the product id return by google 

        }
    });

           }
         class DownloadFileFromURL extends AsyncTask<ArrayList<String>, String, ArrayList<String>> {

ArrayList<String> fileExist = new ArrayList<String>();

@Override
protected void onPreExecute() {
    super.onPreExecute();
    showDialog();// this call to progress bar method display
  }

  @SuppressLint("NewApi")
@Override
protected ArrayList<String> doInBackground(ArrayList<String>... passing) {
     Log.d("download file :", "In download file method");
      }



   protected void onProgressUpdate(String... progress) {
    // setting progress percentage
    //Log.e("Downlaod file", "progress update");
    pDialog.setProgress(Integer.parseInt(progress[0]));
        }


    @Override
    protected void onPostExecute(ArrayList<String> result) {
    // dismiss the dialog after all file is downloaded
    if(fileExist.size()>0)
    {
        // if file exist array contain items than some videos are already avaliable in device so we display toast message to user 
        for(int i=0;i<fileExist.size();i++)
        { 
            // we used databasehelper object and called getvideoName method to fetch video name

        }
    }

     dissmissDialog(); // after displying message close the progress bar   
       }    

            protected Dialog showDialog() {

pDialog = new ProgressDialog(con);
 pDialog.setIndeterminate(false);
 pDialog.setMessage("Téléchargement en cours Veuillez patienter s'il vous plait...."+(i+1)+"/"+Number_of_file);
 pDialog.setMax(100);
  pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  pDialog.setCancelable(false);
  pDialog.show();
  return pDialog;


       }

     protected void dissmissDialog() {

      pDialog.dismiss();
       }

感谢任何帮助,谢谢

1 个答案:

答案 0 :(得分:2)

setOnCancelListener的{​​{1}}属性设置为ProgressDialog

false

否则,当活动进入后台时,progressDialog的视图将被取消(因为他们称之为mProgressDialog.setCancelable(false);