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?

有帮助吗?

解决方案

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>

其他提示

I am aware of one way to do it. However, it will require you to initiate the download from a URL.

The simple idea behind is to use

getContentLength()

to see the content length of the app in bytes.

Then, you can simply use

YouInputStream.read(TheData)

to see the progress of the download.

There is actually a much more detailed answer on how to do this over at Download a file with Android, and showing the progress in a ProgressDialog

On that note, maybe you can get access to the socket on which the app is being downloaded and then follow the same steps from above. I don't have any experience with this tho.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top