2012-07-12 74 views
1

我創建了一個帶有倒計時器的小型應用程序,用於撲克盲注,但我注意到當鎖定屏幕打開時,計時器將從開始重置。屏幕鎖定後重置計時器

我怎樣才能解決這個問題?

這是類的代碼:

public class timer extends Activity { 
     /** Called when the activity is first created. */ 

     TextView contoallarovescia; 
     TextView clicca; 
     public boolean touchattivo=false; 
     public int giro=1; 
     public int girosecondi=8*60; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 

      this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.timer); 

      contoallarovescia = (TextView) this.findViewById(R.id.contoallarovescia); 
      Typeface typeFace=Typeface.createFromAsset(getAssets(),"font/LCDMB___.TTF"); 
      contoallarovescia.setTypeface(typeFace); 

      clicca = (TextView) this.findViewById(R.id.clicca); 
      chiamatimer(girosecondi*1000,1000); 

     } 

     @Override 
     public boolean onTouchEvent(MotionEvent event) { 
      // MotionEvent object holds X-Y values 


      if (touchattivo) 
      { 

       chiamatimer(girosecondi*1000,1000); 

      } 

      return super.onTouchEvent(event); 
     } 


     public void chiamatimer(int secondi, int ciclo) 
     { 


      touchattivo=false; 
      clicca.setVisibility(View.INVISIBLE); 

      new CountDownTimer(secondi, ciclo) { 




       public void onTick(long millisUntilFinished) { 

        int sec =(int) (millisUntilFinished/1000); 


        String result = String.format("%02d:%02d", sec/60, sec % 60); 


        contoallarovescia.setText(result); 
       } 

       public void onFinish() { 


        String result = String.format("%02d:%02d", 0, 0); 
        giro++; 

        if (giro==5) 
        { 

         girosecondi=6*60; 

        } 

        contoallarovescia.setText(result); 

        clicca.setVisibility(View.VISIBLE); 
        touchattivo=true; 

        try { 
          Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
          Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); 
          r.play(); 
         } catch (Exception e) {} 




       } 
       }.start(); 

     } 

} 

TNX!

+0

你壓倒onPause嗎? – Addison 2012-07-12 15:42:31

+0

發佈您的代碼。 – 2012-07-12 15:43:09

回答

0

你需要看看Android文檔中的應用程序生命週期 - 在這種情況下,你建議我會想象你的應用程序只是被關閉並由android重新啓動 - 因此你的onCreate被再次調用並且定時器被重置。

Android應用程序不是簡單的對象,你需要應付的一部分是opsys在需要的時候推動你。

特別看看onPause,以及如何將結束時間存儲在稍後傳遞給onCreate的包中。這將解決您當前的錯誤。

+0

但計時器不會暫停,但繼續倒計時,並當計數到0時給我報警。 – 2012-07-12 16:27:54

+0

也許這可以幫助我? http://www.java2s.com/Open-Source/Android/Timer/multitimer-android/com/cycleindex/multitimer/CountDownTimerWithPause.java.htm – 2012-07-12 16:35:35

3
pm = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE); 
     pmWL = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Tag"); 
     pmWL.acquire(); 

     mKeyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); 
     mKeyguardLock = mKeyguardManager.newKeyguardLock("activity_classname"); 
     mKeyguardLock.disableKeyguard(); 
+0

開放式電源管理器和開放式鍵盤管理器的拳頭。 – 2012-08-31 07:12:43