Android 中在有序廣播中添加自定義權(quán)限的實(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(); } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android判斷是否有拍照權(quán)限的實(shí)例代碼
- Android掃描二維碼時(shí)出現(xiàn)用戶禁止權(quán)限報(bào)錯問題解決辦法
- Android判斷用戶是否允許了攝像頭權(quán)限實(shí)例代碼
- Android無需申請權(quán)限撥打電話的兩種方式
- Android.permission.MODIFY_PHONE_STATE權(quán)限問題解決辦法
- 詳解Android權(quán)限管理之Android 6.0運(yùn)行時(shí)權(quán)限及解決辦法
- Android權(quán)限管理之Permission權(quán)限機(jī)制及使用詳解
- Android6.0動態(tài)申請權(quán)限所遇到的問題小結(jié)
- Android 中的危險(xiǎn)權(quán)限詳細(xì)整理
相關(guān)文章
Android開發(fā)模仿qq視頻通話懸浮按鈕(實(shí)例代碼)
這篇文章主要介紹了Android開發(fā)模仿qq視頻通話懸浮按鈕功能的實(shí)例代碼,需要的的朋友參考下2017-02-02Android ANR(Application Not Responding)的分析
這篇文章主要介紹了Android ANR(Application Not Responding)的分析的相關(guān)資料,這里說明什么原因出現(xiàn)應(yīng)用程序的強(qiáng)制關(guān)閉,并說明該如何避免,需要的朋友可以參考下2017-08-08Android自定義View展開菜單功能的實(shí)現(xiàn)
這篇文章主要介紹了Android自定義View展開菜單功能的實(shí)現(xiàn),需要的朋友可以參考下2017-06-06Android自定義view實(shí)現(xiàn)雪花特效實(shí)例代碼
實(shí)現(xiàn)雪花的效果其實(shí)也可以通過自定義View的方式來實(shí)現(xiàn)的,而且操作上也相對簡單一些,下面這篇文章主要給大家介紹了關(guān)于Android自定義view實(shí)現(xiàn)雪花特效的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12android檢測網(wǎng)絡(luò)連接狀態(tài)示例講解
網(wǎng)絡(luò)的時(shí)候,并不是每次都能連接到網(wǎng)絡(luò),因此在程序啟動中需要對網(wǎng)絡(luò)的狀態(tài)進(jìn)行判斷,如果沒有網(wǎng)絡(luò)則提醒用戶進(jìn)行設(shè)置2014-02-02Android屬性動畫實(shí)現(xiàn)布局的下拉展開效果
這篇文章主要為大家詳細(xì)介紹了Android屬性動畫實(shí)現(xiàn)布局的下拉展開效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07android studio 使用adb 命令傳遞文件到android 設(shè)備的方法
這篇文章主要介紹了android studio 使用adb 命令傳遞文件到android 設(shè)備的相關(guān)資料,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11Android 2.3.7.r1 camera錄像過程中按menu菜單鍵時(shí)會停止錄像
android GB版本的camera錄像過程中按“菜單”鍵會停止錄像,改成錄像時(shí)按menu鍵不做處理,具體修改方法如下,感興趣的朋友可以參考下哈2013-06-06