android定时任务总结

一、Handler + Runnable

    private void testHandler() {
        // 开始启动任务
        MyApplication.getMyApplication().mHandler.post(handlerRunnable);
    }

    // 用handler的延迟任务
    private Runnable handlerRunnable = new Runnable() {
        public void run() {
            MyApplication.getMyApplication().mHandler.postDelayed(this, 3 * 1000);
            Log.d(TAG, "handlerRunnable..." + System.currentTimeMillis());
            tvRes.setText("handlerRunnable...");
        }
    };

二、AlarmManager

    /**
     * 测试AlarmManager
     * AlarmManager是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent。
     * 简单的说就是我们设定一个时间,然后在该时间到来时,AlarmManager为我们广播一个我们设定的Intent,
     * 通常我们使用 PendingIntent,PendingIntent可以理解为Intent的封装包,
     * 简单的说就是在Intent上在加个指定的动作。
     * 在使用Intent的时候,我们还需要在执行startActivity、startService或sendBroadcast才能使Intent有用。
     * 而PendingIntent的话就是将这个动作包含在内了。
     */
    private void testAlarmManager() {
        Log.d(TAG, "testAlarmManager()...");
        AlarmManager alarmManager = (AlarmManager) MyApplication.getMyApplication().getSystemService(Context.ALARM_SERVICE);

        // 1、启动activity的intent
        Intent intent = new Intent(this,TestAlarmActivity.class);
        // 2、启动receiver的intent
        Intent intent_broadcast = new Intent(this,TestReceiver.class);
        intent_broadcast.putExtra("name","name_param_testreceiver");
        // 3、启动service的intent
        Intent intent_service = new Intent(MyApplication.getMyApplication(), TestService.class);
        intent_service.putExtra("name","name_param_testservice");

        PendingIntent pendingIntentActivity = PendingIntent.getActivity(MyApplication.getMyApplication(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent pendingIntentBroad = PendingIntent.getBroadcast(MyApplication.getMyApplication(), 0, intent_broadcast, PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent pendingIntentService = PendingIntent.getService(MyApplication.getMyApplication(), 0, intent_service, PendingIntent.FLAG_CANCEL_CURRENT);

        // 定时任务的执行时间
        long triggerAtMillis = TimeUtils.date2Millis(new Date()) + 5 * 1000;

        Log.d(TAG, "triggerAtMillis = " + triggerAtMillis);
        if (alarmManager != null) {
            // 启动activity来执行定时任务
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntentActivity);
            // 启动广播来执行定时任务
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntentBroad);
            // 启动service来执行定时任务
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntentService);
        }
    }

三、倒计时CountDownTimer

   private void testCountDownTimer() {
        /*
         * 第一个参数是倒计时的总时长
         * 第二个参数是倒计时间隔的时间
         * 调用start()开始倒计时
         * 调用cancel()结束倒计时
         */
        CountDownTimer countDownTimer = new CountDownTimer(10 * 1000, 1 * 1000) {
            @Override
            public void onTick(long millisUntilFinished) { // 这个是每次间隔指定时间的回调
                // 这里可以更新UI
                Log.d(TAG, "onTick(), millisUntilFinished = " + millisUntilFinished);
                tvRes.setText("countDownTimer time = " + millisUntilFinished / 1000);
            }

            @Override
            public void onFinish() {  // 这个是倒计时结束的回调
                Log.d(TAG, "onFinish(),这里倒计时结束");
                cancel(); // 必须要写这个吗?如果不写会怎么?
                tvRes.setText("countDownTimer 倒计时结束");
            }
        };
        countDownTimer.start();
    }

四、Timer+TimerTask

private Timer timer1 = new Timer();
    private Timer timer2 = new Timer();
    // 测试timertask
    private void testTimerTask(){
        // 这个能循环执行
        timer1.schedule(timerTask1,5 * 1000,5 * 1000);
        // 指定时间执行,当前时间的10S以后执行
        timer2.schedule(timerTask2,TimeUtils.getDate(TimeUtils.getNowMills() + 10 * 1000, 0,TimeConstants.MSEC));
    }

    // 每一个timertask都是一个单独的线程?
    TimerTask timerTask1 = new TimerTask() {
        @Override
        public void run() {
            Log.d(TAG,"timerTask1 run...");
        }
    };

    TimerTask timerTask2 = new TimerTask() {
        @Override
        public void run() {
            Log.d(TAG,"timerTask2 run...");
        }
    };

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值