Android中怎么在有序廣播中添加自定義權(quán)限-創(chuàng)新互聯(lián)

這篇文章給大家介紹Android 中怎么在有序廣播中添加自定義權(quán)限,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

創(chuàng)新互聯(lián)建站公司2013年成立,先為浦北等服務建站,浦北等地企業(yè),進行企業(yè)商務咨詢服務。為浦北企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。

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

有序廣播說明:

有序廣播因為要處理消息的處理結(jié)果,所以要復雜一些。

* 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 > <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 > <uses-permission android:name="test.permission.TEST"/></span> 
</manifest>

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

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在接收到廣播的時候,如果想要添加一些自己的東西進去,可以先創(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)于Android 中怎么在有序廣播中添加自定義權(quán)限就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

名稱欄目:Android中怎么在有序廣播中添加自定義權(quán)限-創(chuàng)新互聯(lián)
標題來源:http://muchs.cn/article16/higgg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、企業(yè)網(wǎng)站制作定制開發(fā)、網(wǎng)站營銷、云服務器、網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化