Yairopro
Yairopro

Reputation: 10360

How to send a custom broadcast action to receivers in manifest?

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, final Intent intent) {
        Log.i("MyReceiver", "MyAction received!");
    }
}

In AndroidManifest.xml (under the application tag)

<receiver android:name=".MyReceiver">
    <intent-filter>
        <action android:name="MyAction" />
    </intent-filter>
</receiver>

MainActivity.Java

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sendBroadcast(new Intent("MyAction"));
    }
}

MyReceiver.onReceive method is never triggered. Did I miss something?

Upvotes: 24

Views: 36536

Answers (5)

Mushahid Gillani
Mushahid Gillani

Reputation: 753

If you have multiple receivers, you can send broadcast to all the receivers using only custom action defined in manifest for that you need to add the following flag while sending broadcast

Note: I have used adb to test it on Android 10, you can add it in application

FLAG_RECEIVER_INCLUDE_BACKGROUND = 0x01000000

adb shell am broadcast -a MyAction -f 0x01000000

Upvotes: 4

Ajay Prajapati
Ajay Prajapati

Reputation: 748

In Kotlin:

val intent = Intent(this, MyBroadCastReceiver::class.java)
intent.addCategory(Intent.CATEGORY_DEFAULT)
intent.action = "my.custom.broadcast"
sendBroadcast(intent)

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1006944

I use Android 8.

Then you have to use an explicit Intent, one that identifies the receiver, such as:

sendBroadcast(new Intent(this, MyReceiver.class).setAction("MyAction"));

See Broacast limitations in Android 8 release docs.

Upvotes: 41

Vinayakkumar
Vinayakkumar

Reputation: 6490

In Android 8 onwords

  • We need to provide the explicite class for handling i.e setcomponent param along with action

Example :

  private void triggerBroadCast(String firstFavApp, String secondFavApp) {
        Intent intent = new Intent("FavAppsUpdated");
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        intent.putExtra("FIRST_FAV_APP", firstFavApp);
        intent.putExtra("SECOND_FAV_APP", secondFavApp);
        intent.setComponent(new
                ComponentName("com.android.systemui",
                "com.android.systemui.statusbar.phone.FavAppsChanged"));

        Log.i(TAG, "Trigger Fav Apps" + firstFavApp + " " + secondFavApp);
        favouriteContract.getAppContext().sendBroadcast(intent);
    }

Below Android 8

  • Only action is enough for receiving Broadcast

   void broadCastParkingStates(Context context) {
        Intent intent = new Intent("ReverseCameraStates");
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        intent.putExtra("PARKING_GUIDE", ReverseCameraPreference.getParkingGuide(context));
        intent.putExtra("PARKING_SENSOR", ReverseCameraPreference.getParkingSensor(context));
        intent.putExtra("TRAJECTORY", ReverseCameraPreference.getTrajectory(context));
        Log.i("BootCompletedReceiver", "Sending Reverse Camera settings states BaordCast");
        Log.i("BootCompletedReceiver", "States Parking:Sensor:Trajectory="
                + intent.getExtras().getBoolean("PARKING_GUIDE")
                + ":" + intent.getExtras().getBoolean("PARKING_SENSOR")
                + ":" + intent.getExtras().getBoolean("TRAJECTORY")
        );
        context.sendBroadcast(intent);
    }

Upvotes: 5

ChRoNoN
ChRoNoN

Reputation: 900

The string itself does not matter, just need to be the same in all places and unique, I use fully qualified name of the constant.

<receiver android:name="com.mypackage.receivers.MyBroadcastReceiver">
    <intent-filter>
        <action android:name="com.mypackage.receivers.MyBroadcastReceiver.ACTION_CUSTOM"/>
    </intent-filter>
</receiver>

The receiver:

package com.mypackage.receivers;

public class MyBroadcastReceiver extends BroadcastReceiver {
    public static final String ACTION_CUSTOM = "com.mypackage.receivers.MyBroadcastReceiver.ACTION_CUSTOM";
@Override
    public void onReceive(Context context, Intent intent) {
      if (ACTION_CUSTOM.equals(intent.getAction())) {
            // do custom action
        }
    }
}

To broadcast the intent:

sendBroadcast(new Intent(MyBroadcastReceiver.ACTION_CUSTOM));

Upvotes: -1

Related Questions