2016-06-21 10 views
0

私はアクティビティを持っており、プログレスバーが最初に起動するタイマーを持っており、そのアクティビティには、ネットワークが無効になっているとネットワーク状態の変化を検出するブロードキャスト受信機があります。私はタイマーとプログレスバーを止めたいと思っています。ネットワークが再度有効になったら、タイマーとプログレスバーが停止した場所から再開したいと思います。アンドロイドでタイマーとプログレスバーを一時停止して再開する方法は?

コード: -

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

私の欲望を編集してください – vishwas

関連する問題