Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly filter Package replaced broadcast

Tags:

I am trying to catch the package replaced broadcast for my app and only my app, but for some reason in my reciever I am the broadcast for every app that is updated. I thought you only needed to set the intent filter in the manifest file to your app, but maybe I am wrong?

Here's my code(manifest):

        <receiver android:name=".UpdateReciever">         <intent-filter>             <action android:name="android.intent.action.PACKAGE_REPLACED" />             <data android:scheme="package" android:path="com.my.app" />         </intent-filter>     </receiver> 

Reciever:

public class AppUpdateReciever extends BroadcastReceiver {      @Override     public void onReceive(Context con, Intent intent) {          //code..         }  } 
like image 938
ninjasense Avatar asked Dec 22 '10 21:12

ninjasense


People also ask

What is the difference between a broadcast receiver and an intent filter?

An IntentFilter specifies the types of intents to which an activity, service, or broadcast receiver can respond to by declaring the capabilities of a component. BroadcastReceiver does not allows an app to receive video streams from live media sources.

Where do I register and unregister broadcast receiver?

Receiver can be registered via the Android manifest file. You can also register and unregister a receiver at runtime via the Context. registerReceiver() and Context. unregisterReceiver() methods.

How do I know if my broadcast receiver is registered?

registerReceiver(BroadcastReceiver,IntentFilter) */ public Intent register(Context context, IntentFilter filter) { try { // ceph3us note: // here I propose to create // a isRegistered(Contex) method // as you can register receiver on different context // so you need to match against the same one :) // example by ...


1 Answers

Add this to your onReceive method:

if (intent.getDataString().contains("com.my.app")){     ... } 

EDIT: Note that registering for ACTION_PACKAGE_REPLACED causes your app to be started up every time any app is updated, if it wasn't already open. I don't know how to avoid this before API 12, but in API 12 you can register for ACTION_MY_PACKAGE_REPLACED so you don't have to filter the intent and your app won't be started unnecessarily by other apps being updated.

like image 99
Tenfour04 Avatar answered Sep 19 '22 16:09

Tenfour04