且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Android中固定时间后如何重复执行任务?

更新时间:2023-11-30 12:16:22

使用以下方法设置重复任务:

Set repeated task using this:

//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {

    @Override
    public void run() {
        //Called each time when 1000 milliseconds (1 second) (the period parameter)
    }

},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
1000);

,如果您想取消任务,只需调用t.cancel(),此处t是您的Timer对象

and if you wanted to cancel the task simply call t.cancel() here t is your Timer object

,您还可以检查答案下方的评论,他们已经提供了有关该信息的简短信息.

and you can also check comment placed below your answer they have given brief information about that.