使用Timer做定时任务

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    
    private Timer timers;
    private TimerTask requestMatch;
    private static int mMatchCount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        scheduleRequestMatch();
    }

    private void cancelMatchSchedule() {
        stopTimer();
    }

    private void scheduleRequestMatch() {
        stopTimer();
        timers = new Timer();
        requestMatch = new MatchRequestTimeTask();
        timers.schedule(requestMatch, 0, 8000);//每隔8秒执行一次 第一不delay 刚打开就执行一次
    }

    public void stopTimer() {
        if (timers != null) {
            timers.cancel();
            timers.purge();
            timers = null;
        }
    }

    private static class MatchRequestTimeTask extends TimerTask {

        @Override
        public void run() {
            mMatchCount++;
            Log.i("aaa", "这是第: " + mMatchCount + "循环");
        }
    }
}

 

Log:

07-24 11:00:38.403 837-881/? I/aaa: 这是第: 1循环
07-24 11:00:46.405 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 2循环
07-24 11:00:54.406 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 3循环
07-24 11:01:02.407 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 4循环
07-24 11:01:10.407 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 5循环
07-24 11:01:18.407 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 6循环
07-24 11:01:26.407 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 7循环
07-24 11:01:34.407 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 8循环
07-24 11:01:42.408 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 9循环
07-24 11:01:50.409 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 10循环
07-24 11:01:58.409 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 11循环
07-24 11:02:06.410 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 12循环
07-24 11:02:14.410 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 13循环
07-24 11:02:22.410 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 14循环
07-24 11:02:30.410 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 15循环
07-24 11:02:38.411 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 16循环
07-24 11:02:46.411 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 17循环
07-24 11:02:54.410 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 18循环
07-24 11:03:02.410 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 19循环
07-24 11:03:10.411 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 20循环
07-24 11:03:18.411 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 21循环
07-24 11:03:26.413 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 22循环
07-24 11:03:34.414 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 23循环
07-24 11:03:42.415 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 24循环
07-24 11:03:50.414 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 25循环
07-24 11:03:58.414 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 26循环
07-24 11:04:06.423 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 27循环
07-24 11:04:14.419 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 28循环
07-24 11:04:22.420 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 29循环
07-24 11:04:30.421 837-881/okhttp.custom.android.testglide I/aaa: 这是第: 30循环

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张海龙_China

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值