Android问题集8.0Android Other

AlarmManager 实现定时任务

2018-08-10  本文已影响112人  Zane_Samuel

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

主要实现思路

第一步开始计时的时候启动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

上一篇下一篇

猜你喜欢

热点阅读