How to Know My Android Application Has Been Upgraded in Order to Reset an Alarm

How to know my Android application has been upgraded in order to reset an alarm?

I've never tried this myself, but what about creating a BroadcastReceiver that listens to the ACTION_PACKAGE_REPLACED Intent?

I've thought about trying this before, but I'm not sure if there's a chicken-and-egg problem with it or not (e.g., does the Intent get sent before the new upgraded application can receive it?). Worth a try, though.

Is there a way to restart a service after upgrading the application on Android?

You are right that you should be using a BroadcastReceiver.

Have a look at this question about restarting Alarms on upgrade; it shows how you can use the ACTION_PACKAGE_REPLACED Intent to see when an application is upgraded.

Is there any way to check if an alarm is already set?

First of all, a little tutorial on how to access previously created alarms:

You can differentiate between alarms by creating each with a unique id such as:

Intent intent = new Intent(this, AlarmReceiverActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,UNIQUE_ID_GOES_HERE, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, triggerAtMillis ,pi);

When you want to access this alarm, you have to create the same PendingIntent with the same unique id. For example, the following will only access an alarm that you created with PendingIntent id 1234. Then it will cancel the previous one and reset it.

Intent intent = new Intent(this, AlarmReceiverActivity.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 1234, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, triggerAtMillis ,pi);

The idea is simple. Keep track of the id's and then use them to access their respective alarms. If you create multiple alarms with same id, the most recent one will cancel the previous.

Coming to your main problem, instead of checking if your alarm is active each time you launch your application, just re-set it in your Activity's onCreate() method. with the same PendingIntent as I described above. This saves you all the hassle of checking if the alarm is previously set or not. Since your aim is to keep the alarm alive, it won't hurt to override the previously set one everytime you launch the application. Just make sure you use the same id to create your PendingIntent.

Do not forget to check if the time for your alarm has already passed or not in order to avoid trying to set an alarm for a past time, which will trigger it immediately.

Let us consider another case: when you turn off your device, all your alarms will be cancelled. This leaves you no option but to set them again at reboot. To do that, you will have to use a BroadcastReceiver.

This answer will help you on how to do that. Just recreate your alarm in the onReceive() method of your BroadcastReceiver as suggested above.

Wake an application after market update

As Commonsware mentions, the ACTION_PACKAGE_REPLACED does the trick. You just need to compare your package name with the data for the intent, otherwise you catch all packages being replaced.

In newer API's (12 on up) there is the ACTION_MY_PACKAGE_REPLACED which is only sent to the application that was replaced.

Does uninstalling an Android Application kill any alarms configured?

If you are using AlarmManager yes it does. As does force closing the app.

How to know my Android application has been upgraded in order to reset an alarm?

I've never tried this myself, but what about creating a BroadcastReceiver that listens to the ACTION_PACKAGE_REPLACED Intent?

I've thought about trying this before, but I'm not sure if there's a chicken-and-egg problem with it or not (e.g., does the Intent get sent before the new upgraded application can receive it?). Worth a try, though.



Related Topics



Leave a reply



Submit