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

前端 未结 6 529
心在旅途
心在旅途 2020-12-15 07:48

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, final Intent intent) {
            


        
相关标签:
6条回答
  • 2020-12-15 08:24

    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
    
    0 讨论(0)
  • 2020-12-15 08:26

    In Kotlin:

    val intent = Intent(this, MyBroadCastReceiver::class.java)
    intent.addCategory(Intent.CATEGORY_DEFAULT)
    intent.action = "my.custom.broadcast"
    sendBroadcast(intent)
    
    0 讨论(0)
  • 2020-12-15 08:30

    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.

    0 讨论(0)
  • 2020-12-15 08:32

    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);
        }
    
    0 讨论(0)
  • 2020-12-15 08:34

    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));
    
    0 讨论(0)
  • 2020-12-15 08:50

    Change the MainActivity's code as follows:

    Intent intent = new Intent(this, MyReceiver.class);
    intent.setAction("MyAction");
    sendBroadcast(intent);
    
    0 讨论(0)
提交回复
热议问题