Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a specific app is being downloaded/installed by the Google Play store?

Many apps provide recommendations to install other applications made by the developer or developer's partners. The recommendations are just links to the Play Store where the recommended app may be installed from.

I don't want to provide a recommendation for app X if:

  • App X is already installed
  • App X is in the process of being installed (e.g. downloading on Google Play)

Detecting if X is installed is easy, but I can't seem to find any API to read downloading/installing state from Google Play. Is there any way to detect if the user is in the process of installing app X?

like image 785
UsAaR33 Avatar asked Nov 01 '22 02:11

UsAaR33


1 Answers

Is there any way to detect if the user is in the process of installing app X?

The closest you'll get to this is by using a NotificationListenerService.

From the docs:

A service that receives calls from the system when new notifications are posted or removed.

But considering this Service was recently added in API level 18 and your use case, you might consider using a BroadcastReceiver and listening for when android.intent.action.PACKAGE_ADDED and android.intent.action.PACKAGE_REMOVED are called. This way once an app is installed or removed, you can do what you want with the package name, like provide a link to the Play Store.

Here's an example:

BroadcastReceiver

public class PackageChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null || intent.getData() == null) {
            return;
        }

        final String packageName = intent.getData().getSchemeSpecificPart();
        // Do something with the package name
    }

}

In your AndroidManifest

<receiver android:name="your_path_to.PackageChangeReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_ADDED" />
        <action android:name="android.intent.action.PACKAGE_REMOVED" />

        <data android:scheme="package" />
    </intent-filter>
</receiver>
like image 200
adneal Avatar answered Nov 12 '22 14:11

adneal