SoFunction
Updated on 2025-03-11

An instance of adding custom permissions to orderly broadcasts in Android

An instance of adding custom permissions to orderly broadcasts in Android

Preface;

Orderly broadcast instructions:

Orderly broadcasting is more complicated because it needs to process the processing results of messages.

* sendOrderedBroadcast(Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras);

If you just want the broadcast to be charged according to priority and don’t care about the processing results, you can use the following version:

* sendOrderedBroadcast(Intent intent, String receiverPermission);

Similarly, in a multi-user environment, you can also choose which user to broadcast:

* sendOrderedBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras);

First of all, we need to customize a permission in the format. The format refers to the permissions that come with the system. As long as it is, if it is not followed, then this permission may not be used.

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:andro 
  package=""> 
  <span style="color:#FF0000;"> <permission 
    android:name="" 
    android:protectionLevel="normal" 
    ></permission></span> 
  <application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
      <intent-filter> 
        <action android:name="" /> 
 
        <category android:name="" /> 
      </intent-filter> 
    </activity> 
 
    <receiver 
      android:name=".MyReceiver" 
      android:enabled="true" 
      android:exported="true"> 
      <intent-filter android:priority="20"> 
        <action android:name="hahaha" /> 
      </intent-filter> 
    </receiver> 
    <receiver 
      android:name=".MyReceiver2" 
      android:enabled="true" 
      android:exported="true"> 
      <intent-filter android:priority="19"> 
        <action android:name="hahaha" /> 
      </intent-filter> 
 
    </receiver> 
  </application> 
 
  <span style="color:#FF0000;"> <uses-permission android:name=""/></span> 
</manifest> 

Then use sendOrderedBroadcast(intent,""); to send an ordered broadcast. Of course, before sending a broadcast, you must specify the priority of the recipient. The higher the priority, the larger the number specified by Android:priority. The range of values ​​is: -1000~1000, which will not be described in detail.

public class MainActivity extends AppCompatActivity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState); 
    setContentView(.activity_main); 
    Button send= (Button) findViewById(); 
    (new () { 
      @Override 
      public void onClick(View v) { 
        Intent intent=new Intent(); 
        ("hahaha"); 
 
        ("msg","A simple message"); 
        sendOrderedBroadcast(intent,""); 
      } 
    }); 
  } 
} 

When the first BroadcastReceiver receives the broadcast, if you want to add some of your own things, you can first create a Bundle object and store the data.
Then use setResultExtras(bundle), add this bundle to the original message.

ublic class MyReceiver extends BroadcastReceiver { 
  public MyReceiver() { 
  } 
 
  @Override 
  public void onReceive(Context context, Intent intent) { 
    (context,"The action of the received Intent is:"+()+"\nThe message content is:"+("msg"),Toast.LENGTH_LONG).show(); 
    Bundle bundle=new Bundle(); 
    ("first","The first message deposited by BroadCast!"); 
    setResultExtras(bundle); 
 
  } 
} 

The next BroadcastReceiver can extract the information through getResultExtras.

ublic class MyReceiver2 extends BroadcastReceiver { 
  public MyReceiver2() { 
  } 
 
  @Override 
  public void onReceive(Context context, Intent intent) { 
    // TODO: This method is called when the BroadcastReceiver is receiving 
    Bundle bundle=getResultExtras(true); 
    String first=("first"); 
    (context,"The first message stored in BroadCast is:"+first,Toast.LENGTH_LONG).show(); 
  } 
} 

Thank you for reading, I hope it can help you. Thank you for your support for this site!