0

I have an app with two different versions (two different package names). One is a free version and the other is a paid version. Obviously the free version will always be installed before the paid version.

I want the free version to be able to detect when the paid version gets installed. I can't use a broadcast receiver with action.PACKAGE_ADDED since that's not allowed in manifest anymore, meaning it cannot run in the background, it can only run when the app is open.

1 Answers1

0

You cannot detect another app's installation for obvious privacy issues, but, you can check whether the app is installed in the device and can open it.

To check if the app is installed in the device, use PackageManager.getPackageInfo() as getPackageInfo("com.app.yourAppName", 0). This will either return the info if there's such an app installed in the device or throw a NameNotFoundException if the app is not installed. Check the code below:

Boolean isappInstalled() {
    try {
        pm.getPackageInfo("com.app.yourApp", 0);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

Call this function in onCreate(), onStart() or in onResume() of your launcher activity and do what you want to do with it.

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50
  • But then I'd have to continuously run a service in the background to check if the package name exists –  May 29 '21 at 20:51
  • @purchaseTest Whatever functions you're gonna do on the free app, you'll do them when the app is open either way, hence why not check the paid app's existence only when the free app is opened. Either, put it on `onCreate()`, so it will check it only when the app is opened or put it in `onResume()` and it will check it even when the app is minimized and switched back to. Don't use a service and keep checking it, bad UX, bad performance and useless as well. Checking this through a service serves no purpose. – Lalit Fauzdar May 30 '21 at 08:19
  • I need to check for the paid app's existence without opening the free app because when a user installs the paid app there's no reason for them to open the free app again. In fact they will quickly uninstall it. –  May 30 '21 at 09:41
  • If that's the case, why not put the logic into the paid app instead of the free app? – Lalit Fauzdar May 30 '21 at 19:00
  • Cause I need the SharedPreferences of the free app to be transferred to the paid app before the paid app gets opened the first time. –  May 30 '21 at 19:06
  • Why not save those kind of details on the server's database and let the user login in the paid app as usual, the data will then be easily transferred without this hassle. Keep the data on the server. – Lalit Fauzdar May 30 '21 at 19:09
  • Yeah that would be very simple but I'm using Firestore server for free and I don't want to use a single extra KB if it's possible or else I get charged a fee. –  May 30 '21 at 19:12
  • Then, I'll suggest look at [this answer](https://stackoverflow.com/a/6030399/8244632), copy the SharedPreferences of the free app when the paid app launches for the first time, you can do it on the SplashScreen as well. – Lalit Fauzdar May 30 '21 at 19:22
  • But they're saying MODE_WORLD_READABLE no longer works? –  May 30 '21 at 19:36