2015-11-07 44 views
-1
CountDownTimer (2000, 0000) { 
    @Override 
    public void onTick(long millisUntilFinished) {} 

    @Override 
    public void onFinish() { 

     txt1.setText("..."); 
     txt1.setVisibility(View.VISIBLE); 
     txt2.setText("..."); 
     txt2.setVisibility(View.VISIBLE); 

     cancel(); //<----it deletes the Countdown Timer after finished 

     } 

}.start(); 

我把cancel()onFinish()。所以當倒計時結束時,它會自行刪除。 但是,當我關閉並重新啓動應用程序時,活動從頭開始,並啓動計時器。 我該如何讓它工作一次?計時器必須保持刪除

+0

'當我關閉並重新啓動應用程序時,活動從頭開始'然後你想要什麼? –

+0

@ρяσѕρєяK'我怎樣才能讓它工作一次?'<------全部閱讀 –

+0

如在答案中@tiny陽光說的是 –

回答

1

不,你不能做this.When重新啓動application一樣,U盤創造一個新timer。如果你想實現它,嘗試存儲在SharedPrefense狀態。

SharedPreferences sharedPreferences = getSharedPreferences("save",MODE_PRIVATE); 

if(!sharedPreferences.getBoolean("opened",false)){ 
    //what you want do once 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 
    editor.putBoolean("opened", true); 
    editor.apply(); 
} 
+0

是的,但是......怎麼樣? –

+0

我已更新我的答案。 –