TimerTask()仍然在后台运行,即使在退出android的应用程序中也是如此

时间:2014-09-23 04:51:59

标签: android timertask

我正在开发一个每1分钟显示一次AddScreen的应用程序,用于TimerTask()概念,

问题是每1分钟就会显示一次AddScreen,即使该应用程序当前未打开已退出

AdSplashScheduleTime.java

public class AdSplashScheduleTime{

    Activity activity;

    public AdSplashScheduleTime(Activity a){
        this.activity=a;

        Timer myTimer = new Timer();

        myTimer.schedule(new TimerTask() {      

            @Override
            public void run() {
                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        Intent viewTargetActivity = new Intent(activity.getApplicationContext(), AdSplashCopy.class);
                        activity.startActivity(viewTargetActivity);
                    }
                });
            }
        },60000, 60000);// 5 minutes        

    }


}

DiamondWorldMenus.java //主屏幕

public class DiamondWorldMenus extends TabActivity implements OnClickListener {

AdSplashScheduleTime scheduleTime;

onCreate(......){

scheduleTime=new AdSplashScheduleTime(this);

}

}

1 个答案:

答案 0 :(得分:0)

当你从一个活动切换到另一个活动时,

取消你的时间任务

if(myTimer != null) {
  myTimer.cancel();
  myTimer.purge()
  myTimer = null;
}