系统闹钟 AlarmManager

来源:互联网 发布:二胡自学 知乎 编辑:程序博客网 时间:2024/05/23 13:39

前段时间遇到一个需求 在这里记录一下爬过的坑

需求是这样的  应用隔多久没打开 就弹一个通知提醒用户 

通过这个需求  我就开始了我的爬坑之旅:

由于是本地通知 则通过设置系统闹钟的方式来唤醒我们的应用 再通过应用来弹notification

获取系统闹钟管理:

AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

然后设置闹钟:

AlarmManager的常用方法有三个:
(1)set(int type,long startTime,PendingIntent pi);
该方法用于设置一次性闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟执行时间,第三个参数表示闹钟响应动作。

(2)setRepeating(int type,long startTime,long intervalTime,PendingIntent pi);
该方法用于设置重复闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟首次执行时间,第三个参数表示闹钟两次执行的间隔时间,第三个参数表示闹钟响应动作。

(3)setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi);
该方法也用于设置重复闹钟,与第二个方法相似,不过其两个闹钟执行的间隔时间不是固定的而已。


三个方法各个参数详悉:

(1)int type: 闹钟的类型,常用的有5个值:AlarmManager.ELAPSED_REALTIME、 AlarmManager.ELAPSED_REALTIME_WAKEUP、AlarmManager.RTC、 AlarmManager.RTC_WAKEUP、AlarmManager.POWER_OFF_WAKEUP。

AlarmManager.ELAPSED_REALTIME表示闹钟在手机睡眠状态下不可用,该状态下闹钟使用相对时间(相对于系统启动开始),状态值为3;

AlarmManager.ELAPSED_REALTIME_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟也使用相对时间,状态值为2;

AlarmManager.RTC表示闹钟在睡眠状态下不可用,该状态下闹钟使用绝对时间,即当前系统时间,状态值为1;

AlarmManager.RTC_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用绝对时间,状态值为0;

AlarmManager.POWER_OFF_WAKEUP表示闹钟在手机关机状态下也能正常进行提示功能,所以是5个状态中用的最多的状态之一,该状态下闹钟也是用绝对时间,状态值为4;不过本状态好像受SDK版本影响,某些版本并不支持;

(2)long startTime: 闹钟的第一次执行时间,以毫秒为单位,可以自定义时间,不过一般使用当前时间。需要注意的是,本属性与第一个属性(type)密切相关,如果第一个参数对 应的闹钟使用的是相对时间(ELAPSED_REALTIME和ELAPSED_REALTIME_WAKEUP),那么本属性就得使用相对时间(相对于 系统启动时间来说),比如当前时间就表示为:SystemClock.elapsedRealtime();如果第一个参数对应的闹钟使用的是绝对时间 (RTC、RTC_WAKEUP、POWER_OFF_WAKEUP),那么本属性就得使用绝对时间,比如当前时间就表示 为:System.currentTimeMillis()。

(3)long intervalTime:对于后两个方法来说,存在本属性,表示两次闹钟执行的间隔时间,也是以毫秒为单位。

(4)PendingIntent pi: 绑定了闹钟的执行动作,比如发送一个广播、给出提示等等。PendingIntent是Intent的封装类。需要注意的是,如果是通过启动服务来实现闹钟提 示的话,PendingIntent对象的获取就应该采用Pending.getService(Context c,int i,Intent intent,int j)方法;如果是通过广播来实现闹钟提示的话,PendingIntent对象的获取就应该采用 PendingIntent.getBroadcast(Context c,int i,Intent intent,int j)方法;如果是采用Activity的方式来实现闹钟提示的话,PendingIntent对象的获取就应该采用 PendingIntent.getActivity(Context c,int i,Intent intent,int j)方法。如果这三种方法错用了的话,虽然不会报错,但是看不到闹钟提示效果。

以下是我使用的方法:

alarm.set(AlarmManager.RTC, System.currentTimeMillis() + Integer.parseInt(intervalTime[position]) *24*60* 60 * 1000, pending);

下面贴出我的代码:

AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);Intent intent = new Intent(this, AlarmService.class);intent.putExtra(AlarmService.EVENT, AlarmService.HAVE_OPEN);PendingIntent pending = PendingIntent.getService(this, 0, intent,        PendingIntent.FLAG_CANCEL_CURRENT);alarm.cancel(pending);alarm.set(AlarmManager.RTC, System.currentTimeMillis() + Integer.parseInt(intervalTime[position]) *24*60* 60 * 1000, pending);

乍一看没啥问题 可是 一旦运行之后 会发现 闹钟竟然不准了  后来多方查找 发现在19以上得使用另外一个方法 于是代码修改为以下样式:

AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);Intent intent = new Intent(this, AlarmService.class);intent.putExtra(AlarmService.EVENT, AlarmService.HAVE_OPEN);PendingIntent pending = PendingIntent.getService(this, 0, intent,        PendingIntent.FLAG_CANCEL_CURRENT);alarm.cancel(pending);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {    alarm.setExact(AlarmManager.RTC, System.currentTimeMillis() + Integer.parseInt(intervalTime[position]) *24*60* 60 * 1000, pending);} else {    alarm.set(AlarmManager.RTC, System.currentTimeMillis() + Integer.parseInt(intervalTime[position]) *24*60* 60 * 1000, pending);}

 alarm.setExact(AlarmManager.RTC, System.currentTimeMillis() + Integer.parseInt(intervalTime[position]) *24*60* 60 * 1000, pending);
这个在19以上 闹钟时间会比较准确,于是果断用上了  以为就这样完了, 不!! 才刚刚开始 

因为我的开发机是4.4的系统 所有运行代码后 额 一切正常 简直完美

但是 

但是

测试机是是神奇的东西

我换了台5.0的三星 手机 对 就是会爆炸的那种,结果问题来了  杀掉应用之后  不弹通知了 

简直是晴天霹雳

后来我换了nexus5 7.0的系统 结果竟然可以 后来多方查证 发现 对于国内的有些手机厂商 为了保护用户的安全 对应用做了权限限制 只有在允许应用自动启动的时候  才允许自动唤醒我们的应用进程  也就是说 我们的应用被用户加入白名单的时候 我们才能唤醒我们的应用 

遇到这样的问题只能束手无策了  但是好在 只有小部分的厂商干了这个事情 想想也是  我如果可以随意的唤醒自己应用的进程 那我可以做多少不为人知的坏事 用户得有多危险。

 好了 下面贴下我的代码  记录一下这个坑:

AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);Intent intent = new Intent(this, AlarmService.class);intent.putExtra(AlarmService.EVENT, AlarmService.HAVE_OPEN);PendingIntent pending = PendingIntent.getService(this, 0, intent,        PendingIntent.FLAG_CANCEL_CURRENT);alarm.cancel(pending);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {    alarm.setExact(AlarmManager.RTC, System.currentTimeMillis() + Integer.parseInt(intervalTime[position]) *24*60* 60 * 1000, pending);} else {    alarm.set(AlarmManager.RTC, System.currentTimeMillis() + Integer.parseInt(intervalTime[position]) *24*60* 60 * 1000, pending);}


public class AlarmService extends Service {    private final String TAG = this.getClass().getSimpleName();    public static final String EVENT = "event";    public static final String HAVE_OPEN = "have_open";    public static final String NO_OPEN_LOOP = "no_open_loop";    @Override    public void onCreate() {        super.onCreate();        PushLog.logD(TAG, "onCreate");    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        PushLog.logD(TAG, "onStartCommand open:" + startId + " " + intent);        if (intent != null) {            String from = intent.getStringExtra(EVENT);            NotifyInfo.LongNoopenApp longNoopenApp = AbConfigManager.getInstance(this).getConfig().notify.long_noopen_app;            noOpen(longNoopenApp);            startNoOpenLoop(from, longNoopenApp);            stopSelf();        }        return super.onStartCommand(intent, flags, startId);    }    @Override    public IBinder onBind(Intent intent) {        return null;    }    @Override    public void onDestroy() {        PushLog.logD(TAG, "onDestroy");        super.onDestroy();    }    private void startNoOpenLoop(String from, NotifyInfo.LongNoopenApp longNoopenApp) {        if (StringUtil.isEmpty(longNoopenApp.interval)) {            return;        }        String interval = longNoopenApp.interval;        String[] intervalTime = interval.split("\\|");        int position = PrefUtils.getInt(PrefConstants.LONGTIMENOOPEN.INTERVAL_POSITION, 0);        if(position<intervalTime.length) {            AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);            Intent loopintent = new Intent(this, AlarmService.class);            loopintent.putExtra(AlarmService.EVENT, NO_OPEN_LOOP);            PendingIntent pending = PendingIntent.getService(this, 0, loopintent,                    PendingIntent.FLAG_CANCEL_CURRENT);            if (from.equals(HAVE_OPEN)) {                alarm.cancel(pending);            }            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {                alarm.setExact(AlarmManager.RTC, System.currentTimeMillis() + Integer.parseInt(intervalTime[position])  *24*60* 60 * 1000, pending);            } else {                alarm.set(AlarmManager.RTC, System.currentTimeMillis() + Integer.parseInt(intervalTime[position])  *24*60* 60 * 1000, pending);            }            PrefUtils.putInt(PrefConstants.LONGTIMENOOPEN.INTERVAL_POSITION, position + 1);        }    }    private void noOpen(NotifyInfo.LongNoopenApp longNoopenApp) {        Analytics.sendUIEvent(AnalyticsEvents.Notification.Send_NativeNoti, "no_open", null);        String title = longNoopenApp.title.getText();        String body = longNoopenApp.content.getText();        NotificationCompat.Builder builder =                new NotificationCompat.Builder(this);        builder.setContentTitle(title);        builder.setContentText(body);        builder.setSmallIcon(R.drawable.ic_white_notification);        builder.setLargeIcon(BitmapFactory.decodeResource(GlobalContext.get().getResources(),                R.mipmap.ic_launcher));        builder.setDefaults(Notification.DEFAULT_SOUND);        builder.setTicker(title);        NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);        PendingIntent pendingIntent;        Intent intent=new Intent(this,SplashActivity.class);        intent.putExtra(SplashActivity.EXTRA_FROM,"alarmService");        pendingIntent = PendingIntent.getActivity(this, 0,                intent, PendingIntent.FLAG_CANCEL_CURRENT);        builder.setAutoCancel(true);        builder.setContentIntent(pendingIntent);        Notification build = builder.build();        manager.notify(1, build);    }}

顺便提一下  在安卓5.0以后  notification 的小图标只能显示黑白的图片  不然就会出现一片空白 :

学习路径:http://blog.csdn.net/guolin_blog/article/details/50945228




0 0
原创粉丝点击