当我在android中更改活动时,进度条消失
我编写代码,通过使用带有进度条的异步任务下载我的视频文件,效果很好。但如果我更改活动或将应用程序放在后台,当我返回到活动(或应用程序)时,进度条消失,但下载仍在后台进行,但我没有获得进度条 我将进度条显示代码放在异步任务的Preexecute方法中,并将dissmiss方法放在asynctask的postexecute中 我在谷歌上搜索存储状态,但在我的情况下,它不会工作 这是我的密码当我在android中更改活动时,进度条消失,android,android-asynctask,android-progressbar,Android,Android Asynctask,Android Progressbar,我编写代码,通过使用带有进度条的异步任务下载我的视频文件,效果很好。但如果我更改活动或将应用程序放在后台,当我返回到活动(或应用程序)时,进度条消失,但下载仍在后台进行,但我没有获得进度条 我将进度条显示代码放在异步任务的Preexecute方法中,并将dissmiss方法放在asynctask的postexecute中 我在谷歌上搜索存储状态,但在我的情况下,它不会工作 这是我的密码 protected void onCreate(Bundle savedInstanceState) {
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();
}
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
PowerManager PowerManager=(PowerManager)getSystemService(Context.POWER\u服务);
partialWakeLock=powerManager.newWakeLock(powerManager.PARTIAL_WAKE_LOCK,“Loneworker-部分唤醒锁”);
setContentView(R.layout.restore);
最终按钮还原=(按钮)findViewById(R.id.restore);
restore.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
new DownloadFileFromURL().execute(listOfStrings);//传递包含google返回的产品id的arraylist
}
});
}
类DownloadFileFromURL扩展异步任务{
ArrayList fileExist=新的ArrayList();
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
showDialog();//对进度条方法display的调用
}
@SuppressLint(“新API”)
@凌驾
受保护的ArrayList doInBackground(ArrayList…通过){
Log.d(“下载文件:”,“在下载文件方法中”);
}
受保护的void onProgressUpdate(字符串…进度){
//设置进度百分比
//Log.e(“下载文件”、“进度更新”);
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@凌驾
受保护的void onPostExecute(ArrayList结果){
//下载所有文件后关闭对话框
如果(fileExist.size()>0)
{
//若文件存在数组包含项目,那个么一些视频在设备中已经可用,所以我们向用户显示toast消息
对于(ProgressDialog
in tofalse
mProgressDialog.setCancelable(false);
否则,当活动转到后台时,progressDialog的视图将被取消(因为它们调用onStop()
)