The introduction

On Android 8.0, you can’t register most broadcasts statically. That is, you can’t register some broadcasts statically in the AndroidManifest file. For example, after testing, the static registration for receiving Android boot broadcasts is still able to receive normally. Links to the Android 8.0 behavior change is introduced: developer.android.com/about/versi… , the preceding links must be through the FQ, of course, we advocate science and the Internet, so the domestic mirror site link: developer. The android, Google. Cn/about/versi… .

The solution

We know that there are two types of broadcasting registration:

  1. Static registration, which means that the BroadcastReceiver is registered in the AndroidManifest file, usually with an action for filtering.

  2. Dynamic registration: Call registerReceiver in Context to dynamically register broadcasts, and unregister broadcasts using unRegisterReceiver.

Since Android 8.0 already limits static registration, we don’t have to bother with it (unless you want to).

Suppose there are two apps A and B, where A is the app that sends the broadcast and B is the app that receives the broadcast

Step 1: Define permissions in an app and use custom permissions

<uses-permission android: />

<permission
   android:
   android:protectionLevel="signature" >
</permission>
Copy the code

Note: Android :protectionLevel= “signature” Android :protectionLevel= “signature” Android :protectionLevel= “signature” Android :protectionLevel= “signature” Android :protectionLevel= “signature” Android :protectionLevel= signature.

About protectionLevel, everyone can refer to the following blog: blog.csdn.net/u013553529/…

Step 2: Define registered broadcasts in THE B app

private static final String BROADCAST_PERMISSION_DISC = "com.cn.customview.permissions.MY_BROADCAST"; private static final String BROADCAST_ACTION_DISC = "com.cn.customview.permissions.my_broadcast"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.broadcast_permissions_activity); ButterKnife.bind(this); BroadcastReceiver receiveBroadCast = new receiveBroadCast (); IntentFilter filter = new IntentFilter(); filter.addAction(BROADCAST_ACTION_DISC); // Only receivers with the same action can receive this broadcast registerReceiver(receiveBroadCast, filter,BROADCAST_PERMISSION_DISC,null); } public class ReceiveBroadCast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(BroadcastPermissionsActivity.this, "receive broadcast", 0).show(); }}Copy the code

Register a broadcast and declare that the broadcast requires the BROADCAST_PERMISSION_DISC permission to receive messages. But our application has already registered this permission. So you have access to it.

At the same time, declare permissions in the AndroidManifest of B app:

<uses-permission android: />
Copy the code

Step 3: Send broadcast in an APP

public void sendBroadcastWithPermissions() { Intent intent = new Intent(); Intent. PutExtra ("data", "this is data from broadcast "+ calendar.getinstance ().get(calendar.second)); intent.setAction(BROADCAST_ACTION_DISC); SendBroadcast (intent,BROADCAST_PERMISSION_DISC); // Send broadcast}Copy the code

Broadcasts that represent receiving messages require the BROADCAST_PERMISSION_DISC permission.

RegisterReceiver (receiveBroadCast, filter,BROADCAST_PERMISSION_DISC,null); RegisterReceiver (receiveBroadCast, filter); Endbroadcast (intent,BROADCAST_PERMISSION_DISC); endbroadcast (intent,BROADCAST_PERMISSION_DISC); We’re still picking up the broadcast. 2.sendBroadcast(intent,BROADCAST_PERMISSION_DISC); Instead of sendBroadcast (intent); You can also receive this message. If the second parameter is dropped, then the broadcast listener does not need any permissions to receive the message.