Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcastreceiver and Paused activity

I have a broadcast receiver registered programatically in a activity. It responds to the PACKAGE_REMOVED intent, that fires when a package gets removed.

The problem is, it doesn't get this message. I think this is due to that the intent is fired when I leave the activity and move to another activity to uninstall a app, so the original activity is paused. Could it be that a paused activity (where the receiver is not unregistered in onPause) also pauses the receiver?

like image 761
Peterdk Avatar asked Oct 25 '11 13:10

Peterdk


People also ask

When would you use a BroadcastReceiver?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.

What is a BroadcastReceiver?

Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.

Is it necessary to unregister BroadcastReceiver?

It's always suggested to register and unregister broadcast receiver programmatically as it saves system resources.

How do I turn off BroadcastReceiver on Android?

To stop receiving broadcasts, call unregisterReceiver(android. content. BroadcastReceiver) . Be sure to unregister the receiver when you no longer need it or the context is no longer valid.


2 Answers

When you register a broadcast receiver programatically in an activity, it will NOT get broadcasts when the activity is paused. The BroadcastReceiver docs are not as clear as they could be on this point. They recommend unregistering on onPause solely to reduce system overhead.

If you want to receive events even when your activity is not in the foreground, register the receiver in your manifest using the receiver element.

like image 150
Chris Avatar answered Sep 23 '22 10:09

Chris


Add a Receiver to your project and you will get this event without even starting your application.

public class TestReciver extends BroadcastReceiver  {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("TestReciver",intent.getAction()+"\n"
                +intent.getDataString()+"\n"
                +"UID: "+intent.getIntExtra(Intent.EXTRA_UID,0)+"\n"
                +"DATA_REMOVED: "+intent.getBooleanExtra(Intent.EXTRA_DATA_REMOVED, false)+"\n"
                +"REPLACING: "+intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)
            );
    }

}

and in your manifest add it like this (Inside your <application> tag):

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

When you use a receiver like this you do not call any register or unregister so it will always be ready to get data.

A note is that this will not work if you let the users move your app to the SD card. If an event is sent when the SD card is unmounted the receiver will not be accessible and you will miss the event.

like image 20
H9kDroid Avatar answered Sep 22 '22 10:09

H9kDroid