我的应用程序中有一个进度条。我正在调用意图服务来下载大尺寸的文件。服务开始下载时,进度条正在正确更新。当我从设备上单击后退按钮时,意图服务仍在后台运行,进度条计数也在后台更新,但是当我再次打开应用程序时,即使进度条计数从哪里开始,进度条也不会更新值我离开了。你能帮我解决这个问题吗?
这是我在布局文件中的进度条,
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:indeterminate="false"
android:max="100"
android:minHeight="50dp"
android:minWidth="200dp"
android:progress="1" />
这是我更新进度条的java文件。进度意图将包含进度更新的当前计数。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sharedPref = getPreferences(Context.MODE_PRIVATE);
progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
updateReceiver = new CheckNetworkConnectivity(
new NetworkReceiverCallback() {
@Override
public void onNetworkReceived(int status) {
networkStaus = status;
if (networkStaus == 0) {
Toast.makeText(
getApplicationContext(),
"Download interrupted due to network issue.",
Toast.LENGTH_SHORT).show();
} else {
onCallService(fileLength, currentDate, downloaded, Constants.downloadStatus);
Toast.makeText(getApplicationContext(),
"Start downloading....",
Toast.LENGTH_SHORT).show();
}
}
});
registerReceiver(updateReceiver, new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION));
activity = this;
}
private void onCallService(long downloaded, String lastModifiedDate, long size, int downloadStatus) {
mReceiver = new DownloadResultReceiver(new Handler());
mReceiver.setReceiver(this);
Intent intent = new Intent(Intent.ACTION_SYNC, null, this,
DownloadBackgroundService.class);
String url = "";
intent.putExtra("url", url);
intent.putExtra("receiver", mReceiver);
intent.putExtra("downloaded", downloaded);
intent.putExtra("downloadStatus", downloadStatus);
intent.putExtra("size", size);
intent.putExtra("lastModifiedDate", lastModifiedDate);
startService(intent);
}
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
int progress = resultData.getInt("progress");
progressBar.setProgress(progress);
progressBar.setMax(100);
progressBar.setText(String.valueOf(progress) + "/100");
progressBar2.invalidate();
progressBar2.setProgress(progress);
progressBar2.isFocused();
Log.d("DownloadService", "Progress bar is updating : " + progress);
}
@Override
protected void onStart() {
progressBar = (TextProgressBar) findViewById(R.id.progressBar1);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.setText(String.valueOf(0) + "/100");
super.onStart();
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(updateReceiver);
}
答案 0 :(得分:0)
请更改:
android:layout_width="wrap_content"
到
android:layout_width="match_parrent"
答案 1 :(得分:0)
如果您可以在日志中看到修改后的值,并且只在输出中看不到它,请尝试progressBar.invalidate();
答案 2 :(得分:0)
当我再次打开应用程序时,即使进度条计数从我离开的位置开始,进度条也不会更新值
当您通过单击后退按钮退出应用程序时,属于活动的每个资源都将被销毁。然后,当您再次打开应用程序时,将再次重新提供您的资源。这意味着当您再次打开应用时,当您致电progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
时,您的进度条将初始化为新实例
我建议解决方案是每次收到下载进度的价值,将其保存到数据库或首选项中,然后在打开应用程序时将其恢复:
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
int progress = resultData.getInt("progress"); // <---- SAVE THIS VALUE
progressBar.setProgress(progress);
progressBar.setMax(100);
progressBar.setText(String.valueOf(progress) + "/100");
progressBar2.invalidate();
progressBar2.setProgress(progress);
progressBar2.isFocused();
Log.d("DownloadService", "Progress bar is updating : " + progress);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sharedPref = getPreferences(Context.MODE_PRIVATE);
progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
progressBar2.setProgress(progress); // GET SAVED VALUE AND SET IT INTO PROGRESS
答案 3 :(得分:0)
错误的帖子。 &#34; onReceiveResult&#34;是一个异步事件,即可能不会在UI线程中。 使用mActivity.runOnUiThread更新UI。