如何向清单中的接收者发送自定义广播操作?

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!");
    }
}

AndroidManifest.xml中(在application标签下)

<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 方法永远不会被触发。 我错过了什么吗?

I use Android 8.

那你就得用一个明确的Intent,一个能识别接收者的,比如:

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

请参阅 Android 8 版本文档中的 Broacast limitations

字符串本身并不重要,只需要在所有地方都相同且唯一即可,我使用常量的完全限定名称。

<receiver android:name="com.mypackage.receivers.MyBroadcastReceiver">
    <intent-filter>
        <action android:name="com.mypackage.receivers.MyBroadcastReceiver.ACTION_CUSTOM"/>
    </intent-filter>
</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
        }
    }
}

广播意图:

sendBroadcast(new Intent(MyBroadcastReceiver.ACTION_CUSTOM));

在Android 8个词中

  • 我们需要提供用于处理的显式 class,即 setcomponent 参数和操作

示例:

  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);
    }

低于Android8

  • 接收广播只需要动作

   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);
    }

在 Kotlin 中:

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

更改 MainActivity 的代码如下:

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

如果您有多个接收者,您可以只使用清单中定义的 自定义操作 向所有接收者发送广播在发送广播时添加以下标志

注意:我在Android10上用adb测试过,你可以在application

中添加

FLAG_RECEIVER_INCLUDE_BACKGROUND = 0x01000000

adb shell am broadcast -a MyAction -f 0x01000000