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

时间:2018-03-09 15:41:37

标签: android broadcastreceiver android-manifest android-broadcast

MyReceiver.java

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

AndroidManifest.xmlapplication标记下)

<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方法。 我错过了什么吗?

6 个答案:

答案 0 :(得分:15)

  

我使用Android 8。

然后你必须使用一个明确的Intent,一个标识接收者,例如:

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

请参阅Android 8发布文档中的Broacast limitations

答案 1 :(得分:1)

在Android 8单词中

  • 我们需要提供显式类来处理即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);
    }

Android 8以下

  • 仅动作就足以接收广播

                                   

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

答案 2 :(得分:1)

如果您有多个接收者,则可以仅使用清单中定义的 自定义操作 将广播发送到所有接收者,因为您需要在发送广播时添加以下标志

注意:我已经使用adb在 Android 10 上进行了测试,您可以将其添加到应用程序中

FLAG_RECEIVER_INCLUDE_BACKGROUND = 0x01000000

adb shell am broadcast -a MyAction -f 0x01000000

答案 3 :(得分:0)

在科特林

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

答案 4 :(得分:0)

更改主要活动代码,如下所示: Intent intent =new Intent(this,MyReceiver.class); intent.setAction("MyAction"); sendBroadcast(intent);

答案 5 :(得分:-1)

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

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