我的應用程序中有一個進度條。我正在調用一個意向服務來下載大文件。進度條在服務開始下載時正確更新。當我從設備上點擊後退按鈕時,意圖服務仍在後臺運行,並且進度條計數也在後臺更新,但是當我再次打開該應用程序時,即使進度條計數從何處開始,進度條也沒有更新該值我離開了。你能幫我解決這個問題嗎?當單擊後退按鈕時進度條沒有更新值
這是具有在佈局文件中的進度條IM,
<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文件,其中IM更新進度。進度意圖將具有當前的進度更新計數。
@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);
}
發佈您的完整Java代碼將更好地幫助 –
已添加@PhanDinhThai – Ravi