Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

android - Intent PACKAGE_ADDED not registering

Hello i am trying to detect application installed so that i can do some analysis on the application and i am using this example i found on stackoverflow to listen for package installs from my current application but nothing is happening in my logcat.

void registerReceiver() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    filter.addDataScheme("package");

}

public void onReceive(Context context, Intent intent) {
    String actionStr = intent.getAction();
    if (Intent.ACTION_PACKAGE_ADDED.equals(actionStr)) {
        Uri data = intent.getData();
        String pkgName = data.getEncodedSchemeSpecificPart();
        //handle package adding...
        Log.i("Logging Service", pkgName);

    }

}

<receiver android:name="RealTimeActivity">
    <intent-filter>
   <category android:name="android.intent.category.DEFAULT" />
    <action android:name="android.intent.action.PACKAGE_ADDED"  />
    <action android:name="android.intent.action.PACKAGE_CHANGED" />
    <action android:name="android.intent.action.PACKAGE_INSTALL" />
    <action android:name="android.intent.action.PACKAGE_REMOVED" />
    <action android:name="android.intent.action.PACKAGE_REPLACED" />
    </intent-filter>
 </receiver>


<uses-permission android:name="android.permission.BROADCAST_PACKAGE_ADDED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_CHANGED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_INSTALL" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REPLACED" />
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Due to a broadcast behavior change since Android 3.1, your app must be started before it can receive the app installation/removal intents. See kabuko's answer in this thread.

The following receiver works for me on a Android 4.0 device (I have an activity in the app, first start the activity i.e. the app is also started, then the receiver can receive the broadcast).

<receiver android:name=".MyReceiver">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_ADDED" />
        <action android:name="android.intent.action.PACKAGE_CHANGED" />
        <action android:name="android.intent.action.PACKAGE_REMOVED" />
        <data android:scheme="package" />
    </intent-filter>
</receiver>

(some apps run a dummy sticky service to keep the app process alive, so that they can receive certain broadcasts)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...