3

I'm creating an Android app that will be packaged and distributed in both lite (free) and full versions across perhaps 5 app stores.

The app contains a Service triggered by AlarmManager.setRepeating(...), so that it fires at an interval configurable by the user, usually between every 5 minutes and hourly.

The trouble I foresee is that a user may wind up with both free and full versions of the app, and potentially from more than one store. What can I do to prevent multiple instances of the Service from being triggered by the AlarmManager? Will it help to make sure the Service name (package name and <service> attribute in the manifest) is the same? Is there a way that one variant of the app can, on being launched for the first time, disable pending intents requested by the other variants?

Craig McMahon
  • 1,550
  • 1
  • 14
  • 36

1 Answers1

2

Not an efficient suggestion: Every time AlarmManager starts your Service, you can check if a newer version (e.g. Full version) is intalled. If it is then cancel your alarm by giving negative intervalMillisecond to AlarmManager.setRepeating(...) method.

Much cleaner approach will be forcing older installs to be removed. As soon as a version of your app starts for the first time, check a list of possible other packages that might be installed before. If there are any remove them. Services belonging them will be removed as well.

Here is how to check existance of a package:

public static boolean isPackageInstalled(Context context, String uri) {
    PackageManager pm = context.getPackageManager();
    boolean installed = false;
    try {
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        installed = true;
    } catch (PackageManager.NameNotFoundException e) {
        installed = false;
        // Not installed
    }
    return installed;
}

How to uninstall a package by calling an intent is described here: Implicit intent to uninstall application?

Community
  • 1
  • 1