我每天午夜都在我的课程中运行Android方法。

时间:2017-02-05 00:53:52

标签: java android scheduled-tasks alarmmanager

我在活动类中有一些方法。我每天午夜都想运行这些方法。我想在活动类中执行此操作:

public void doStuff() {

//the Date and time at which you want to execute
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormatter .parse("2012-07-06 13:05:45");

//Now create the time and schedule it
Timer timer = new Timer();

//Use this if you want to execute it once
timer.schedule(new MyTimeTask(), date);

//Use this if you want to execute it repeatedly
//int period = 10000;//10secs
//timer.schedule(new MyTimeTask(), date, period );
}
public static class MyTimeTask extends TimerTask
{

    public void run()
    {
        //calculate new times
        //and relaunch service

    }
}

但作为一个静态类,我无法访问公共的方法和变量。那么我可以使用警报管理器来执行此操作吗?我可以在哪里接收警报以便我可以运行我的任务?

0 个答案:

没有答案