gpt4 book ai didi

android - android中用户点击后退按钮时如何保存进度条的进度状态

转载 作者:行者123 更新时间:2023-11-29 14:01:36 24 4
gpt4 key购买 nike

我想知道当用户在进度进行时单击后退按钮会发生什么。我想到,当用户退出当前 Activity 并返回到上一个 Activity 或任何其他应用程序时,当前进度应该保存。我已经编写了用于下载文件并在用户点击同步按钮时显示进度条的代码。具体如下:

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] + "%");
}


}

我需要一些代码放在上面提到的代码之间,这样当用户按下后退按钮时 Activity 退出时它会保存进度条的状态,当 Activity 打开时它会从停止的地方恢复.请帮助我。

最佳答案

您可以在退出时将进度条状态保存在静态参数中...

@Override
public void onBackPressed() {
state=progressbar.getprogress();
super.onBackPressed();
}

关于android - android中用户点击后退按钮时如何保存进度条的进度状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9239109/

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