我想知道当进度进行时用户点击后退按钮会发生什么。我想到当用户退出当前活动并返回上一个活动或任何其他应用程序时,当前进度应该保存。我编写了下载文件的代码,并在用户点击同步按钮时显示进度条。它如下:
public class DownloadVideoTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... downloadurl) {
int count;
try{
URL url = new URL(downloadurl[0]);
URLConnection connection = url.openConnection();
connection.connect();
int lengthOfFile = connection.getContentLength();
Log.d(TAG, "Length of file: " + lengthOfFile);
InputStream inputStream = new BufferedInputStream(url.openStream());
OutputStream outputStream = new FileOutputStream("/sdcard/drona_video.mp4");
byte data[] = new byte[1024];
long total = 0;
while((count = inputStream.read(data))!= -1){
total += count;
publishProgress("" + (int)((total*100)/lengthOfFile));
outputStream.write(data, 0, count);
}
dbAdapter.insertVideoBytesInVideoDownloadsTable(id, data);
//bundle.putByteArray("videoBytesArray", data);
outputStream.flush();
outputStream.close();
inputStream.close();
}catch(Exception e){
return null;
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//showDialog(DIALOG_DOWNLOAD_PROGRESS);
syncBtn.setEnabled(false);
progressBar.setMax(100);
progressBar.setVisibility(View.VISIBLE);
txtProgressPercentage.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String result) {
//dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
//mProgressDialog.dismiss();
progressBar.setVisibility(View.INVISIBLE);
progressBar.setSaveEnabled(true);
txtProgressPercentage.setVisibility(View.INVISIBLE);
syncBtn.setVisibility(View.INVISIBLE);
playBtn.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(String... progress) {
Log.d(TAG, progress[0]);
//mProgressDialog.setProgress(Integer.parseInt(progress[0]));
progressBar.setProgress(Integer.parseInt(progress[0]));
txtProgressPercentage.setText(progress[0] + "%");
}
}
我需要在上面提到的代码之间放置一些代码,以便在用户按下后退按钮时活动退出时保存进度条的状态,当活动打开时,它会从已停止的位置恢复。请帮帮我。
答案 0 :(得分:0)
退出时,可以将进度条状态保存在静态参数中。
@Override
public void onBackPressed() {
state=progressbar.getprogress();
super.onBackPressed();
}