我像下面的代码一样使用AlarmManager(没有用于AlarmIntent的setAcion()方法)
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), duty.getId(), alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
我研究了一下,发现几乎所有示例都喜欢为警报意图添加一个动作,例如下面的代码,但是我仍然不知道这是否真的必要吗?
alarmIntent.setAction("com.example.myalarm.action");
//add intent-filer for receiver declatation
<receiver
android:name="com.example.receivers.AlarmReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.example.myalarm.action" />
</intent-filter>
</receiver>