我编写代码通过使用带有进度条的异步任务下载我的视频文件,它工作正常..但是如果我更改我的活动或将我的应用程序置于后台并且当我回到我的活动(或应用程序)时,进度条会消失但是下载仍在后台进行,但我没有看到进度条...
我将进度条显示代码放在异步任务的 Preexecute 方法中,并将 dissmiss 方法放在异步任务的 postexecute 中。
我在谷歌上搜索存储状态,但在我的情况下它不起作用..
这是我的代码
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();
}
感谢您的帮助