How to make my app receive broadcast when other ap

2020-01-24 05:11发布

I want to make an app that can receive broadcast when other apps on the device are installed or removed.

my code

in manifset:

<receiver android:name=".apps.AppListener">
    <intent-filter android:priority="100">
         <action android:name="android.intent.action.PACKAGE_INSTALL"/>
         <action android:name="android.intent.action.PACKAGE_ADDED"/>  
         <action android:name="android.intent.action.PACKAGE_REMOVED"/>
    </intent-filter>
</receiver>

in AppListener:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AppListener extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
    // TODO Auto-generated method stub
    Log.v(TAG, "there is a broadcast");
    }
}

but I can't receive any broadcast. I think this problem is due to app permissions, any idea?

Thanks for helps.

3条回答
地球回转人心会变
2楼-- · 2020-01-24 05:38

Great Answers, just one small thing left:

On every App update first ACTION_PACKAGE_REMOVED will be called followed by ACTION_PACKAGE_ADDED- if you wish to ignore these events, just add it on your onReceive():

if(!(intent.getExtras() != null &&
    intent.getExtras().containsKey(Intent.EXTRA_REPLACING) &&
    intent.getExtras().getBoolean(Intent.EXTRA_REPLACING, false))) {

    //DO YOUR THING
}

This is from the docs:

EXTRA_REPLACING Added in API level 3 String EXTRA_REPLACING Used as a boolean extra field in ACTION_PACKAGE_REMOVED intents to indicate that this is a replacement of the package, so this broadcast will immediately be followed by an add broadcast for a different version of the same package. Constant Value: "android.intent.extra.REPLACING"

查看更多
做自己的国王
3楼-- · 2020-01-24 05:45

In your manifest:

<receiver android:name=".apps.AppListener">
    <intent-filter android:priority="100">
         <action android:name="android.intent.action.PACKAGE_INSTALL"/>
         <action android:name="android.intent.action.PACKAGE_ADDED"/>  
         <action android:name="android.intent.action.PACKAGE_REMOVED"/>
    </intent-filter>
</receiver>

Add the line before the intent-filter tag

<data android:scheme="package"/>

So your manifest should look like this:

<receiver android:name=".apps.AppListener">
    <intent-filter android:priority="100">
         <action android:name="android.intent.action.PACKAGE_INSTALL"/>
         <action android:name="android.intent.action.PACKAGE_ADDED"/>  
         <action android:name="android.intent.action.PACKAGE_REMOVED"/>
         <data android:scheme="package"/> 
    </intent-filter>
</receiver>

Am not sure about the PACKAGE_REMOVED intent in that if its actually is available.

查看更多
看我几分像从前
4楼-- · 2020-01-24 06:02

You must eliminate android.intent.action.PACKAGE_INSTALL as it is deprecated and no longer recommended, because it is just for the system. Everything else is perfect and I would recommend that instead of 100, put 999, the documentation does not give maximum or minimum number to use, the larger the number, the higher priority will have your receiver for that intent. Sorry for the translator. I speak and write in Spanish. Information

<receiver android:name=".apps.AppListener">
<intent-filter android:priority="999">
     <action android:name="android.intent.action.PACKAGE_ADDED"/>  
     <action android:name="android.intent.action.PACKAGE_REMOVED"/>
     <data android:scheme="package"/> 
</intent-filter>

查看更多
登录 后发表回答