一、关于AlarmManager
AlarmManager类提供对系统警报服务的访问。这些允许您安排应用程序在将来的某个时间运行。当警报响起时,Intent系统会广播为其注册的警报,如果目标应用程序尚未运行,则会自动启动它。设备处于休眠状态时会保留已注册的警报(如果设备在此期间关闭,则可以选择将设备唤醒),但如果设备关闭并重新启动,则会清除AlarmManager的任务。
AlarmManager 系统提供的一个定时任务管理器,通过AlarmManager 提供的定时任务,可以在约定的时间发送广播,启动服务,启动Activity等等。AlarmManager是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播指定的Intent。使用它既可以指定单次执行的定时任务,也可以指定重复运行的周期性任务;
从API 19开始,AlarmManager的机制都是非准确传递,操作系统将会转换闹钟,来最小化唤醒和电池使用,所以时间可能没那么精确;AlarmManager具有唤醒CPU的功能,可以保证每次需要执行特定任务时CPU都能正常工作, 当cpu处于休眠状态时,或者当前app进程被结束掉,之前的定时任务也会保留,但是由于系统定制厂商的不同,系统方面可能在此状态下拦截定时事件,需要系统后台作出修改,否则定时任务将失效,不能正常提醒
二、关于设备重新启动会清除AlarmManager的任务的问题处理
设备重新启动后,可以将之前记录的定时时间,重新设定AlarmManager定时任务,创建广播接收器,监听开机广播并开启一个后台服务去执行重新设置定时任务
@Override protected void onHandleWork(@NonNull Intent intent) { if (intent.getAction().equals(ACTION_ALARMINTENTSERVICE)) { Cursor cursor = getContentResolver() .query(NoteContent.Notes.CONTENT_URI, NoteContent.Notes.PROJECTION, NoteContent.Notes.COLUMN_NAME_HAS_ALERT + " = ?", new String[]{"" + NoteContent.Notes.HASALERTED}, null); Cursor laterCursor = getContentResolver() .query(NoteContent.Notes.CONTENT_URI, NoteContent.Notes.PROJECTION, NoteContent.Notes.COLUMN_NAME_HAS_ALERT + " = ?", new String[]{"" + NoteContent.Notes.LATER_REMIND}, null); Log.d(TAG, "onHandleIntent: " + laterCursor.getCount()); if (cursor != null) { if (cursor.moveToFirst() && cursor.getCount() > 0) { while (cursor.getPosition() < cursor.getCount()) { NotesListAdapterDataCache itemDataCache = new NotesListAdapterDataCache(); NoteListDataItem dataItem = itemDataCache .getNoteItemData(cursor); if (dataItem != null && dataItem.mId > 0l && dataItem.mHasAlert == NoteContent.Notes.HASALERTED) { sendAlarmBroadcast(ContentUris.withAppendedId(NoteContent.Notes.CONTENT_URI, dataItem.mId), dataItem.mAlertTime); } cursor.moveToNext(); } } if (!cursor.isClosed()) { cursor.close(); } } if (laterCursor != null) { if (laterCursor.moveToFirst() && laterCursor.getCount() > 0) { while (laterCursor.getPosition() < laterCursor.getCount()) { NotesListAdapterDataCache itemDataCache = new NotesListAdapterDataCache(); NoteListDataItem dataItem = itemDataCache .getNoteItemData(laterCursor); if (dataItem != null && dataItem.mId > 0l && dataItem.mHasAlert == NoteContent.Notes.LATER_REMIND){ SharedPreferences pref = getSharedPreferences("alarmdata",Context.MODE_PRIVATE); Long mLaterTime = pref.getLong("alarmtime",0); Log.d(TAG, "onHandleIntent: " + "mLaterTime = " + mLaterTime + "||" + dataItem.mId); sendLaterRemindBroadcast(ContentUris.withAppendedId(NoteContent.Notes.CONTENT_URI, dataItem.mId),mLaterTime); } laterCursor.moveToNext(); } } if (!laterCursor.isClosed()) { laterCursor.close(); } } } }
private void sendAlarmBroadcast(Uri uri, long alertTime) { Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class); intent.setAction(AlarmReceiver.ACTION_ALERT); intent.setData(uri); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); PendingIntent pi = PendingIntent.getBroadcast( getApplicationContext(), 0, intent, 0); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, pi); } else { alarmManager.setExact(AlarmManager.RTC_WAKEUP, alertTime, pi); } } private void sendLaterRemindBroadcast(Uri uri,long laterTime) { Intent mIntent = new Intent(getApplicationContext(), AlarmReceiver.class); mIntent.setAction(AlarmReceiver.ACTION_LATER_REMIND); mIntent.setData(uri); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, mIntent, 0); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { alarmManager.set(AlarmManager.RTC_WAKEUP, laterTime, pi); } else { alarmManager.setExact(AlarmManager.RTC_WAKEUP, laterTime, pi); } }