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.
A broadcast receiver will always get notified of a broadcast, regardless of the status of your application. It doesn't matter if your application is currently running, in the background or not running at all.
Context-registered receivers receive broadcasts as long as their registering context is valid. For an example, if you register within an Activity context, you receive broadcasts as long as the activity is not destroyed. If you register with the Application context, you receive broadcasts as long as the app is running.
There are two ways to make a broadcast receiver known to the system: One is declare it in the manifest file with this element. The other is to create the receiver dynamically in code and register it with the Context. registerReceiver() method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With