我有一个活动并且有进度条的计时器初始启动,并且该活动中有一个广播接收器,如果网络被禁用,它将检测网络状态变化。我想停止计时器和进度条,当再次启用网络时,我想从计时器和进度条停止的地方恢复。我怎么能这样做,请告诉。
代码: -
private final BroadcastReceiver m_InternetChecker = new BroadcastReceiver() {/*Receiver to check Network state*/
@Override
public void onReceive(Context context, Intent intent) {
init();
}
};
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.otp_auto_verified);
/*Registering Broadcast receiver*/
IntentFilter m_internetFilter = new IntentFilter();// creating object of Intentfilter class user for defining permission
m_internetFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");// action to check Internet connection
getApplicationContext().registerReceiver(m_InternetChecker, m_internetFilter);// register receiver....
}
@SuppressLint("SetTextI18n")
private void init() {// initialize view controls
if (NetworkUtil.isConnected(getApplicationContext())) {
m_AlertDialog.dismiss();
//Initialize a new CountDownTimer instance
long m_MillisInFuture = 30000;// timer value
long m_CountDownInterval = 1000;// timer break up
m_oTimer = new CountDownTimer(m_MillisInFuture, m_CountDownInterval) {
public void onTick(long millisUntilFinished) {
millis = millisUntilFinished;
@SuppressLint("DefaultLocale") String hms = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
System.out.println(hms);
tv.setText(hms);
//Another one second passed
//Each second ProgressBar progress counter added one
m_n_ProgressStatus += 1;
m_timerProgress.setProgress(m_n_ProgressStatus);
}
public void onFinish() {// when timer finished
/*This is new change ....check if app is in backround or foreground*/
/*if app is in background*/
if (NetworkUtil.isAppIsInBackground(getApplicationContext())) {
System.out.println("In Background");
Intent i = new Intent(COtpAutoVerificationScreen.this, COtpManualVerificationScreen.class);
startActivity(i);
finish();
} else {
System.out.println("In Foreground");
CSnackBar.getInstance().showSnackBarError(findViewById(R.id.mainLayout), "Otp not found", getApplicationContext());
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(COtpAutoVerificationScreen.this, COtpManualVerificationScreen.class);
startActivity(i);
finish();
}
}, 3500);
}
}
}.start();// start timer
// retreive progress bar count........
int progressBarMaximumValue = (int) (m_MillisInFuture / m_CountDownInterval);
//Set ProgressBar maximum value
//ProgressBar range (0 to maximum value)
m_timerProgress.setMax(progressBarMaximumValue);
} else {
m_oTimer.cancel();
m_n_ProgressStatus = 0;
m_AlertDialog.show();
}
}
答案 0 :(得分:0)
您实际上是在onTick(long)函数中更新进度条。因此,一旦计时器暂停,进度条也是相同的。不幸的是,我找不到任何暂停它的方法。
我会在onTick处理程序中添加一些东西,以便在类变量中保存计时器的进度,例如剩下的毫秒数。
然后一旦网络被禁用,您可以调用计时器的cancel()并保存毫秒左右的值和进度条状态(值)。
一旦启用网络活动,就会创建一个新的计时器,并保留剩余的毫秒数。