Intent filter for PACKAGE_ADDED for a specific pac

2019-06-09 03:52发布

问题:

In my Android app, I'm catching the PACKAGE_ADDED broadcast:

<receiver android:name=".OnAppInstall">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_INSTALL" />
        <action android:name="android.intent.action.PACKAGE_ADDED" />
        <data android:scheme="package" />
    </intent-filter>
</receiver>

My app is only concerned with installation of a specific companion app. The data string in the intent goes: package:com.acme.myapp. The intent data Uri is an instance of OpaqueUri; getHost(), getPath(), and getQuery() all return nulls.

Can I somehow make an intent filter that only allows intents for one specific package ID?

回答1:

Try the following.

<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"
      android:sspPrefix="com.acme.myapp" />
</intent-filter>

This matches with the corresponding API call here.