1)android.intent.action.BOOT_COMPLETED
工作正常,只调用广播onReceive一次
2)当用户手动更改多次调用广播onReceive的时间时,android.intent.action.TIME_SET
会多次调用广播
3)当用户手动更改日期时,android.intent.action.DATE_CHANGED
工作正常,但是这个动作也调用了android.intent.action.TIME_SET,因此广播onReceive调用了两次
在我的情况下,我想取消现有的闹钟并设置新闹钟,但现在它多次调用onReceive,结果取消现有闹钟并多次设置新闹钟&我收到多个通知
<receiver android:name=".MyReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.DATE_CHANGED" />
<action android:name="android.intent.action.TIME_SET" />
</intent-filter>
</receiver>
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
private String TAG="MyReceiver";
private static final String ACTION_LOCAL_NOTIFICATION = "com.example.android.alarm.ACTION_LOCAL_NOTIFICATION";
@Override
public void onReceive(Context context, Intent intent) {
if(ACTION_LOCAL_NOTIFICATION.equalsIgnoreCase(intent.getAction())) {
Log.d("MyReceiver", "Send Local Notification");
}else if("android.intent.action.BOOT_COMPLETED".equalsIgnoreCase(intent.getAction()) ||
"android.intent.action.TIME_SET".equalsIgnoreCase(intent.getAction()) ||
"android.intent.action.DATE_CHANGED".equalsIgnoreCase(intent.getAction()))
{
Log.d("MyReceiver", "Cancel exhisting Alarm & Set new Alarm ");
DBOperation databaseOperations=new DBOperation(context);
databaseOperations.openSqliteDB();
ArrayList<MyAlarmItem> alarmList=databaseOperations.fetchAllAlarm();
databaseOperations.closeSqliteDB();
int alarmListSize=alarmList.size();
AppAlarmManager alarmManager=new AppAlarmManager(context);
for (int i=0;i<alarmListSize;i++)
{
MyAlarmItem alarmItem=alarmList.get(i);
alarmManager.RestoreAlarm(alarmItem);
}
}
}