国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 中在有序廣播中添加自定義權限的實例

2019-12-12 02:26:41
字體:
來源:轉載
供稿:網友

Android 中在有序廣播中添加自定義權限的實例

前言;

有序廣播說明:

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

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

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

* sendOrderedBroadcast(Intent intent, String receiverPermission);

同樣,在多用戶環境下,也可以選擇給哪個用戶發廣播:

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

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

<?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");就可以發送有序廣播了,當然發送廣播之前還要指定一下接受者的優先級,優先級越高,android:priority指定的數字就越大。取值的范圍是:-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在接收到廣播的時候,如果想要添加一些自己的東西進去,可以先創建一個Bundle對象,并且存入數據。
然后通過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消息內容是:"+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();   } } 

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涿州市| 都兰县| 萨迦县| 安国市| 泉州市| 响水县| 团风县| 沾化县| 竹北市| 肃南| 城口县| 会昌县| 凤阳县| 德令哈市| 习水县| 洛南县| 广南县| 长沙县| 巴林右旗| 台州市| 宁德市| 穆棱市| 洪湖市| 孟连| 繁昌县| 上思县| 新余市| 巴青县| 五指山市| 资阳市| 满洲里市| 项城市| 增城市| 秭归县| 丰城市| 神木县| 株洲县| 铜梁县| 上饶县| 隆昌县| 车险|