I've a countdown timer. In the end of 60 secs I started a new activity. It works fine. But if I press home button or back button before the countdown timer ends, after that particular time the activity starts automatically even if I'm in home screen. I finished the activity using this.finish(); But still doesn't work. Please provide me a solution.
4 回答
            0        
        
		
当用户按下主页或返回按钮时,计时器线程在后台运行,因此您必须覆盖 onDestroy() 方法并停止其中的计时器。
于 2012-08-09T07:21:48.897   回答
    
    
            0        
        
		
您必须根据您的要求处理您的计时器onPause()/onResume()或您的活动。onDestroy()
例如。如果您只想停止计时器,请使用:
timer.cancel()在onDestroy()
像这样的东西:
@Override
protected void onDestroy() {
timer.cancel();
super.onDestroy();
    于 2012-08-09T05:18:03.127   回答
    
    
            0        
        
		
您需要在代码中禁用主页和返回按钮,因为后台线程会继续运行,因此即使您在主屏幕上,您的活动也会开始。我给你这个代码来禁用你的活动的主页按钮。
 @Override
 public void onAttachedToWindow() {
 this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
 super.onAttachedToWindow();
 }
这将在您进行该活动时禁用您的主页按钮。
希望这对您有所帮助。
于 2012-08-09T05:18:05.827   回答
    
    
            0        
        
		
您可以覆盖活动的 OnPause 事件。当活动被发送到后面时,完成它。
    @Override
    public void onPause(){
            super.onPause();
            timer.cancel();
            this.finish();
    }
    于 2012-08-09T05:18:17.400   回答