Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make my app receive broadcast when other applications are installed or removed

Tags:

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.

like image 240
bmavus Avatar asked Jun 28 '12 13:06

bmavus


People also ask

Does broadcast receiver work in background?

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.

How a broadcast is received in application?

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.

How do you declare a broadcast receiver in manifest?

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.


1 Answers

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.

like image 161
t0mm13b Avatar answered Sep 27 '22 21:09

t0mm13b