2015-11-07 81 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

我已更新我的答案。 –