gpt4 book ai didi

android - 当我在 android 中更改 Activity 时,进度条消失

转载 作者:行者123 更新时间:2023-11-29 21:35:08 29 4
gpt4 key购买 nike

我编写的代码通过使用带有进度条的异步任务来下载我的视频文件并且它工作正常..但是如果我改变我的 Activity 或将我的应用程序置于后台并且当我回到我的 Activity (或应用程序)而不是进度条消失了,但下载仍在后台进行,但我没有看到进度条...

我把我的进度条显示代码放在异步任务的Preexecute方法中,dissmiss方法放在asynctask的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();
}

感谢您的帮助

最佳答案

ProgressDialogsetOnCancelListener 属性设置为 false

mProgressDialog.setCancelable(false); 

否则,当 Activity 进入后台时,progressDialog 的 View 将被关闭(因为它们调用了 onStop())

关于android - 当我在 android 中更改 Activity 时,进度条消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18573745/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com