AlarmManager 实现定时任务

作者: Zane_Samuel | 来源:发表于2018-08-10 09:58 被阅读112次

    因为自己项目需要一个计时任务,考虑到种种原因,项目大概需要的是这样的 只要进程不被杀死,每间隔30分钟向后台发送一次服务,将地图的地位点发送到后台,考虑到如果用handler 和Thread的或者是Timer 的话考虑一个当时屏幕熄灭或者锁屏的情况下可能会出现问题。所以最后的选择方案是AlarmManager(闹钟服务)、BroadCastReceiver、Service实现了一个计时器
    ` ```

    主要实现思路

    • 首先启动一个Service
    • Service onStartCommand()方法中,只要执行完毕你需要的操作,然后写一个闹钟服务多长时间发送一次
    • 在广播的方法onReceive()在调起服务这样就会轮询一个计时任务(注意:Android 8.0 不再允许后台service直接通过startService方式去启动)也就是讲后台不能直接startService()后面上代码会提到这是我在写的时候遇到的一个坑
      此🐶来震这篇文章
      #行了,废话说了直接上代码

    第一步开始计时的时候启动Service

    Intent intent = new Intent(this, LongRunningService.class);
     startService(intent);
    

    第二步在ServiceonStartCommand()方法中启动轮询

    public void run() {
                  Log.d("LongRunningService", "executed at " + new Date().
                            toString());
                }
            }).start();
            AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
            //int anHour = 10* 1000; // 10秒
            int anHour = 1000; // 1秒
            long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
            //要跳转的广播
            Intent i = new Intent(this, AlarmReceiver.class);
            //通过PendingIntent跳转  这里不多做解释  
            PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
            manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
    

    第三步在BroadcastReceiver的onReceiver()中

    //只是适配8.0之前的
    Intent i = new Intent(context, LongRunningService.class);
     context.startService(i);
    

    优化第三步

     Intent i = new Intent(context, LongRunningService.class);
            //判断是否是8.0版本
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //这是8.0以后的版本需要这样跳转
                context.startForegroundService(i);
            } else {
                context.startService(i);
            }
    

    但是这样启动Service以后必须在Service中执行startForeground(-1,new NotificationCompat().build())
    否则会报ANR;
    适配8.0版本第二步的优化

    new Thread(new Runnable() {
                @Override
                public void run() {
                  Log.d("LongRunningService", "executed at " + new Date().
                            toString());
                }
            }).start();
            AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
            //int anHour = 10* 1000; // 10秒
            int anHour = 1000; // 10秒
            long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
            Intent i = new Intent(this, AlarmReceiver.class);
            PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
            manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
            String CHANNEL_ID = "my_channel_01";
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this, CHANNEL_ID)
    
                            .setContentTitle("My notification")
                            .setContentText("Hello World!");
    
            startForeground(-1,mBuilder.build());
         //   stopForeground(true);
            stopSelf();
    

    项目不需要适配8.0手机不需要改动
    源码下载:
    https://github.com/biao941122/Alarm

    相关文章

      网友评论

        本文标题:AlarmManager 实现定时任务

        本文链接:https://www.haomeiwen.com/subject/kwzhbftx.html