-1

In my application, I am setting Alarms and is working fine.when I am trying to install the code again without uninstalling the previous package my alarms are being deleted.I would like to restore my alarms until unless I delete alarms.

I have come across somewhere( Android - Alarm lost after app update ) saying that we should use android.intent.action.MY_PACKAGE_REPLACED in a receiver such that I could set alarms again when it is broadcasted.whereas this broadcast is not triggered everytime I do a re-run!

Please help me out with your valuable suggestions :)

Thanks in advance.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
sudha
  • 193
  • 13
  • Yes you have to set alarm again and also when device boots all alarms are cancelled you have to set them again after boot process finishes – Satender Kumar Feb 07 '18 at 13:21
  • while reboot was fine whereas I couldn't figure out the solution when i do a rerun ..all alarms are getting cancelled :( could you help me out either with a process or any code snippet that helps? – sudha Feb 07 '18 at 13:23

1 Answers1

1

In your manifest add those receivers

<receiver
android:name=".BootReceiver"
android:enabled="false">
      <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED"></action>
      </intent-filter>
</receiver>

<receiver
      android:name=".InstallReceiver"
      android:exported="true" >
      <intent-filter>
           <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
      </intent-filter>
</receiver>

Now create these two receivers

public class BootReceiver extends BroadcastReceiver {
    SirvedAlarmReceiver alarm = new SirvedAlarmReceiver();
    UpdateAlarmReceiver updateAlarmReceiver = new UpdateAlarmReceiver();

    @Override
    public void onReceive(Context context, Intent intent) {

            // Set alarm here

    }
}

public class InstallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

                // Set alarm here

    }
}

Now in both classes onReceive method set your alarm

Satender Kumar
  • 627
  • 4
  • 7
  • When I am re-running my application during development, "android.intent.action.MY_PACKAGE_REPLACED" - InstallReceiver is getting called unpredictably. I am unable to understand its behavior. – sudha Feb 07 '18 at 13:43