2016-06-21 47 views
0

我有一個活動,並有進度條內啓動計時器,並有一個廣播接收器在該活動中檢測網絡狀態的變化,如果網絡禁用。我想停止計時器和進度條,並且當​​網絡再次啓用時,我想從計時器和進度條停止的位置恢復。請問我該怎麼做,請告訴。如何在android中暫停和恢復計時器和進度條?

代碼: -

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

你實際上是在更新您的onTick進度條(長)功能。所以一旦計時器暫停,進度條也是一樣的。不幸的是我找不到任何方法來暫停它。

我會給onTick處理程序添加一些內容,以保存定時器在類變量中的進度,例如剩餘的毫秒數。

然後,一旦網絡被禁用,您可以調用定時器的cancel()並保存毫秒左值和進度條狀態(值)。

一旦啓用了網絡的活動創建一個新的計時器並保留剩餘的毫秒數。

+0

請編輯我的wuestion – vishwas