Asked  7 Months ago    Answers:  5   Viewed   23 times

I'd like to know what is the best practice/way of programmatically register a broadcast receiver. I want to register specific receivers according to user choice.

As the registration is done through the manifest file, I'm wondering if there's a proper way to achieve this in code.



It sounds like you want to control whether components published in your manifest are active, not dynamically register a receiver (via Context.registerReceiver()) while running.

If so, you can use PackageManager.setComponentEnabledSetting() to control whether these components are active:, int, int)

Note if you are only interested in receiving a broadcast while you are running, it is better to use registerReceiver(). A receiver component is primarily useful for when you need to make sure your app is launched every time the broadcast is sent.

Tuesday, June 1, 2021
answered 7 Months ago

In your service's onCreate():

private Handler handler; // Handler for the separate Thread

HandlerThread handlerThread = new HandlerThread("MyNewThread");
// Now get the Looper from the HandlerThread so that we can create a Handler that is attached to
//  the HandlerThread
// NOTE: This call will block until the HandlerThread gets control and initializes its Looper
Looper looper = handlerThread.getLooper();
// Create a handler for the service
handler = new Handler(looper);
// Register the broadcast receiver to run on the separate Thread
registerReceiver (myReceiver, intentFilter, broadcastPermission, handler);
Friday, July 30, 2021
answered 5 Months ago

If you wound up completely uninstalling and reinstalling the app, the problem is that you have no activity.

Starting with Android 3.1, applications are installed in a "stopped" state, where no broadcast receivers will work until the user manually launches an activity. This is an anti-malware move. I blogged about this ~9 months ago.

Friday, August 6, 2021
answered 4 Months ago

Static or Dynamic? We may assume that reminders may be set for some longer periods of time after which it will be triggered. Therefore, it is safer to use static broadcast receiver in your case.

In your Manifest file:

<receiver android:name=".YourBroadcastReceiver"/>

Separate receiver for each reminder? Actually, no. You can point all of the reminders to one static receiver and it will handle all of them with no problems. If you want to separate between types of reminders that will need to do different actions, you may put some stringExtra to your intent and extract that in if-else statement in your broadcast receiver. That's one way.

If reminders were set to significantly long date in future: You might know that you are setting reminders using alarmManager. However, all of the alarms are deleted if system is rebooted. Therefore, you may consider adding some sort of back to your reminders. You can store information about the reminders in SharedPreferences/SQLite db or any other method you prefer as long as you can easily read and write data from it. Then you need to reset alarms after system reboot. For this purposes you need to add one more broadcastReceiver that will listen for system reboot action being completed and run when it receives it. Then you recreate your alarms there or run separate intentService that will recreate alarms.

In your Manifest file:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<receiver android:name=".BootCompletedReceiver">
        <action android:name="android.intent.action.BOOT_COMPLETED" />
Wednesday, September 1, 2021
answered 3 Months ago

There are some Broadcast Limitations in Oreo, it no longer supports to registering broadcast receivers for implicit broadcasts in app manifest. And NEW_OUTGOING_CALL is one of them, read here

You can use PHONE_STATE action for your purpose as it hasn't categorized as a implicit broadcasts yet

public class StateReceiver extends BroadcastReceiver {

  public void onReceive(Context context, Intent intent) {
   // will trigger at incoming/outgoing call

    try {
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    catch (Exception e){

In manifest,

        <receiver android:name=".StateReceiver">
            <action android:name="android.intent.action.PHONE_STATE" />

Also you need to add and check READ_PHONE_STATE permission

Thursday, November 4, 2021
K. Gl.
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :