1

1) android.intent.action.BOOT_COMPLETED is working fine it calls the broadcast onReceive only once

2) android.intent.action.TIME_SET is calls the broadcast on multiple times when user manually changes the time it calls the broadcast onReceive multiple times

3) android.intent.action.DATE_CHANGED is working fine when user manually changes the date but along with this action this android.intent.action.TIME_SET is too called so the broadcast onReceive called twice

In my case i want to my cancel existing alarm and set the new alarm but now it calls the onReceive multiple times which results cancel existing alarm and set the new alarm multiple times & am getting multiple notification

 <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);
            }
        }

}
Sarath Kumar
  • 1,922
  • 3
  • 19
  • 43

0 Answers0