欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 中在有序廣播中添加自定義權(quán)限的實(shí)例

 更新時(shí)間:2017年07月19日 15:56:28   投稿:lqh  
這篇文章主要介紹了Android 中在有序廣播中添加自定義權(quán)限的實(shí)例的相關(guān)資料,這里對有序廣播的用法進(jìn)行了詳細(xì)介紹并附有簡單實(shí)例,需要的朋友可以參考下

Android 中在有序廣播中添加自定義權(quán)限的實(shí)例

前言;

有序廣播說明:

有序廣播因?yàn)橐幚硐⒌奶幚斫Y(jié)果,所以要復(fù)雜一些。

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

如果只是想讓廣播可以按優(yōu)先級來收取,并不在意處理的結(jié)果,可以用下面的版本:

* sendOrderedBroadcast(Intent intent, String receiverPermission);

同樣,在多用戶環(huán)境下,也可以選擇給哪個用戶發(fā)廣播:

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

首先我們要在AndroidManifest.xml中自定義一個權(quán)限,格式參考系統(tǒng)自帶的權(quán)限,Android.permission.XXXXX,只要是xxx.peimission.XXXXXX就行,如果不按照這個格式,那么這個權(quán)限可能沒法使用。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  package="com.example.lsj.broadcasttest"> 
  <span style="color:#FF0000;"> <permission 
    android:name="test.permission.TEST" 
    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="android.intent.action.MAIN" /> 
 
        <category android:name="android.intent.category.LAUNCHER" /> 
      </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="test.permission.TEST"/></span> 
</manifest> 

然后使用sendOrderedBroadcast(intent,"test.permission.TEST");就可以發(fā)送有序廣播了,當(dāng)然發(fā)送廣播之前還要指定一下接受者的優(yōu)先級,優(yōu)先級越高,android:priority指定的數(shù)字就越大。取值的范圍是:-1000~1000這個就不詳細(xì)敘述了。

public class MainActivity extends AppCompatActivity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button send= (Button) findViewById(R.id.send); 
    send.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        Intent intent=new Intent(); 
        intent.setAction("hahaha"); 
 
        intent.putExtra("msg","一個簡單的消息"); 
        sendOrderedBroadcast(intent,"test.permission.TEST"); 
      } 
    }); 
  } 
} 

第一個BroadcastReceiver在接收到廣播的時(shí)候,如果想要添加一些自己的東西進(jìn)去,可以先創(chuàng)建一個Bundle對象,并且存入數(shù)據(jù)。
然后通過setResultExtras(bundle),把這個bundle添加到原來的消息中,

ublic class MyReceiver extends BroadcastReceiver { 
  public MyReceiver() { 
  } 
 
  @Override 
  public void onReceive(Context context, Intent intent) { 
    Toast.makeText(context,"接收到的Intent的Action為:"+intent.getAction()+"\n消息內(nèi)容是:"+intent.getStringExtra("msg"),Toast.LENGTH_LONG).show(); 
    Bundle bundle=new Bundle(); 
    bundle.putString("first","第一個BroadCast存入的消息!"); 
    setResultExtras(bundle); 
 
  } 
} 

下一個BroadcastReceiver通過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=bundle.getString("first"); 
    Toast.makeText(context,"第一個BroadCast存入的消息為:"+first,Toast.LENGTH_LONG).show(); 
  } 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論