<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Broadcast 使用詳解

          共 6290字,需瀏覽 13分鐘

           ·

          2020-09-24 22:15

          和你一起終身學(xué)習(xí),這里是程序員Android

          經(jīng)典好文推薦,通過閱讀本文,您將收獲以下知識點:

          一、Broadcast概述
          二、Broadcast的注冊
          三、Broadcast的注冊類型
          四、靜態(tài)注冊開機廣播的實現(xiàn)
          五、動態(tài)監(jiān)聽亮滅屏幕廣播實現(xiàn)
          六、廣播的發(fā)送方法

          一、Broadcast概述

          在了解廣播之前,我們先了解Broadcast繼承關(guān)系 ,
          Broadcast的繼承關(guān)系如下:

          java.lang.Object
          ? android.content.BroadcastReceiver

          Broadcast是?Android?四大組件之一,是一種廣泛運用在應(yīng)用程序之間異步傳輸信息的機制。Broadcast?本質(zhì)上是一個Intent?對象,差別在于Broadcast可以被多個?BroadcastReceiver處理。BroadcastReceiver?是一個全局監(jiān)聽器,通過它的?onReceive()?可以過濾用戶想要的廣播,進(jìn)而進(jìn)行其它操作。

          BroadcastReceiver?默認(rèn)是在主線程中執(zhí)行,如果onReceiver()方法處理事件超過10s,則應(yīng)用將會發(fā)生ANR(Application Not Responding),此時,如果建立工作線程并不能解決此問題,因此建議:如處理耗時操作,請用?Service代替。

          BroadcastReceiver的主要聲明周期方法onReceiver(),此方法執(zhí)行完之后,BroadcastReceiver實例將被銷毀。

          二、Broadcast的注冊

          Broadcast?屬于Android四大組件之一,必須在Androidmainfest.xml中注冊.

          Broadcast?注冊方法如下:

                  <receiver
          android:name="ReceiverMethod"
          android:enabled="true"
          android:exported="true">

          <intent-filter>
          <action android:name="String....." />
          intent-filter>
          receiver>

          注意:
          如不注冊,將導(dǎo)致無法接收處理廣播消息

          三、Broadcast的注冊類型

          廣播的注冊分兩種(靜態(tài)注冊、動態(tài)注冊),一種在Androidmainfest.xml中靜態(tài)注冊,另一種是在Java代碼中動態(tài)注冊。

          1.靜態(tài)注冊

          一些系統(tǒng)發(fā)送的廣播需要在Androidmainfest.xml中靜態(tài)注冊,例如 開機廣播,apk狀態(tài)改變廣播,電量狀態(tài)改變廣播等。這些靜態(tài)注冊的廣播,通常在Androidmainfest.xml中攔截特定的字符串。

          靜態(tài)注冊廣播的方法如下:


          <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.programandroid"
          android:versionCode="1"
          android:versionName="1.0" >

          ... ...
          <receiver
          android:name="com.programandroid.BroadcastReceiver.NotificationReceived"
          android:enabled="true"
          android:exported="true" >

          <intent-filter>
          <action android:name="Notification_cancel" />
          <action android:name="Notification_music_pre" />
          <action android:name="Notification_music_play" />
          <action android:name="Notification_music_next" />
          intent-filter>
          receiver>
          ... ...

          2.動態(tài)注冊廣播

          在Java中動態(tài)注冊廣播,通常格式如下:

            //動態(tài)注冊廣播
          registerReceiver(BroadcastReceiver, IntentFilter);

          四、靜態(tài)注冊開機廣播的實現(xiàn)

          1. 靜態(tài)開機廣播實現(xiàn)

          p ublic class BootReceiverMethod extends BroadcastReceiver {
          @Override
          public void onReceive(Context context, Intent intent) {
          // 接收開機廣播處理事情,比如啟動服務(wù)
          Intent mStartIntent = new Intent(context, StartServiceMethods.class);
          context.startService(mStartIntent);
          }
          }

          2.靜態(tài)注冊開機廣播

                  
          <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

          <receiver
          android:name=".component.BroadcastReceiver.BootReceiverMethod"
          android:enabled="true"
          android:exported="true">

          <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED" />
          intent-filter>
          receiver>

          五、動態(tài)監(jiān)聽亮滅屏幕廣播實現(xiàn)

          1.動態(tài)注冊亮滅屏實現(xiàn)

          動態(tài)注冊廣播方法是registerReceiver()
          注意:
          在廣播中動態(tài)注冊廣播請注意一定要使用context.getApplicationContext(),防止context?為空 ,引起空指針異常。

          動態(tài)注冊亮滅屏實現(xiàn)實現(xiàn)如下:

          public class ScreenOnOffReceiver {

          public static void ReceiverScreenOnOff(Context context) {
          IntentFilter screenOffFilter = new IntentFilter();
          screenOffFilter.addAction(Intent.ACTION_SCREEN_OFF);
          screenOffFilter.addAction(Intent.ACTION_SCREEN_ON);
          BroadcastReceiver mScreenOnOffReceiver = new BroadcastReceiver() {

          @Override
          public void onReceive(Context context, Intent intent) {
          // TODO Auto-generated method stub
          String action = intent.getAction();
          if (action.equals(Intent.ACTION_SCREEN_OFF)) {

          Toast.makeText(context, "接收屏幕熄滅廣播", Toast.LENGTH_SHORT).show();

          }
          if (action.equals(Intent.ACTION_SCREEN_ON)) {

          Toast.makeText(context, "接收屏幕點亮廣播", Toast.LENGTH_SHORT).show();
          }
          }
          };
          /**
          * context.getApplicationContext()
          * 在廣播中注冊廣播時候需要注意,防止context 為空 ,引起空指針異常
          * **/

          // 2.動態(tài)注冊廣播的方法
          context.registerReceiver(mScreenOnOffReceiver, screenOffFilter);
          }
          }

          六、廣播的發(fā)送方法

          廣播的方法有以下三種:
          1.無序sendBroadcast(intent)
          2.有序sendOrderedBroadcast()
          3.持續(xù)sendStickyBroadcast())

          1.發(fā)送無序廣播的方法

          發(fā)送無序廣播在Android?中很常見,是一種一對多的關(guān)系,主要通過?sendBroadcast(intent);發(fā)送廣播。

          • 1.發(fā)送自定義廣播案例


          廣播說白了就是一個帶Action等字符串標(biāo)記的Intent。發(fā)送自定義廣播舉例如下:

                  Intent customIntent=new Intent();
          customIntent.setAction("SendCustomBroadcast");
          sendBroadcast(customIntent);
          • 2.接收自定義廣播的方法

          當(dāng)用戶對某些廣播感興趣的話,此時可以獲取此廣播,然后在onReceive方法中處理接收廣播的一下操作。


          public class CustomBroadcast extends BroadcastReceiver {
          public CustomBroadcast() {
          }

          @Override
          public void onReceive(Context context, Intent intent) {

          if (intent.getAction().equals("SendCustomBroadcast")){
          Toast.makeText(context,"自定義廣播接收成功:Action:SendCustomBroadcast",Toast.LENGTH_SHORT).show();
          }
          }
          }

          注意
          自定義廣播是在Androidmanfest.xml中靜態(tài)注冊的。

          2.發(fā)送有序廣播

          廣播在Android是有優(yōu)先級的,優(yōu)先級高的廣播可以終止或修改廣播內(nèi)容。發(fā)送有序廣播的方法如下sendOrderedBroadcast(intent,"str_receiver_permission");

          • 1.發(fā)送自定義有序廣播


                  Intent customOrderIntent=new Intent();
          customOrderIntent.setAction("SendCustomOrderBroadcast");
          customOrderIntent.putExtra("str_order_broadcast","老板說:公司每人發(fā) 10 個 月餅");
          sendOrderedBroadcast(customOrderIntent,"android.permission.ORDERBROADCAST");

          廣播屬于四大組件,一定要在AndroidMainfest.xml中注冊。

          • 2.有序廣播靜態(tài)注冊


          接收有序廣播的靜態(tài)注冊方法如下:

                 <receiver
          android:name=".component.BroadcastReceiver.CustomHightBrodcast"
          android:enabled="true"
          android:exported="true"
          >

          <intent-filter android:priority="1000">
          <action android:name="SendCustomOrderBroadcast" />
          intent-filter>
          receiver>

          <receiver
          android:name=".component.BroadcastReceiver.CustomMiddleBroadcast"
          android:enabled="true"
          android:exported="true"
          >

          <intent-filter android:priority="500">
          <action android:name="SendCustomOrderBroadcast" />
          intent-filter>
          receiver>
          <receiver
          android:name=".component.BroadcastReceiver.CustomLowerBroadcast"
          android:enabled="true"
          android:exported="true"
          >

          <intent-filter android:priority="100">
          <action android:name="SendCustomOrderBroadcast" />
          intent-filter>
          receiver>
          • 3.有序廣播處理,高優(yōu)先級廣播可以優(yōu)先處理


          public class CustomHightBrodcast extends BroadcastReceiver {
          public CustomHightBrodcast() {
          }

          @Override
          public void onReceive(Context context, Intent intent) {

          if (intent.getAction().equals("SendCustomOrderBroadcast")) {
          Toast.makeText(context, intent.getStringExtra("str_order_broadcast"), Toast.LENGTH_SHORT).show();
          Bundle bundle=new Bundle();
          bundle.putString("str_order_broadcast","經(jīng)理說:公司每人發(fā) 5 個 月餅");
          // 修改廣播傳輸數(shù)據(jù)
          setResultExtras(bundle);
          }
          }
          }
          • 4.中優(yōu)先級的廣播后序處理


          public class CustomMiddleBroadcast extends BroadcastReceiver {
          public CustomMiddleBroadcast() {
          }
          @Override
          public void onReceive(Context context, Intent intent) {

          if (intent.getAction().equals("SendCustomOrderBroadcast")) {
          Toast.makeText(context, getResultExtras(true).getString("str_order_broadcast"), Toast.LENGTH_SHORT).show();
          Bundle bundle=new Bundle();
          bundle.putString("str_order_broadcast","主管說:公司每人發(fā) 2 個 月餅");
          setResultExtras(bundle);
          }
          }
          }
          • 5.低優(yōu)先級廣播最后處理


          public class CustomLowerBroadcast extends BroadcastReceiver {
          public CustomLowerBroadcast() {
          }

          @Override
          public void onReceive(Context context, Intent intent) {
          if (intent.getAction().equals("SendCustomOrderBroadcast")) {
          String notice= getResultExtras(true).getString("str_order_broadcast");
          Toast.makeText(context,notice, Toast.LENGTH_SHORT).show();
          // 終止廣播繼續(xù)傳播下去
          abortBroadcast();
          }
          }
          }

          注意 :
          有序廣播需要聲明并使用權(quán)限

          • 1.聲明使用權(quán)限

           
          <uses-permission > android:name="android.permission.ORDERBROADCAST" />
          • 2.聲明權(quán)限

           
          <permission>
          android:name="android.permission.ORDERBROADCAST"/>

          在有序廣播中高優(yōu)先級的廣播接收廣播,可以修改數(shù)據(jù),然后傳給低優(yōu)先級的廣播。

          3.發(fā)送持續(xù)廣播(已經(jīng)被棄用)

          粘性廣播會在Android系統(tǒng)中一直存在,不過隨著?Android系統(tǒng)的不斷更新,此方法逐漸被拋棄,使用方法如下:sendStickyBroadcast(intent);

          至此,本篇已結(jié)束。轉(zhuǎn)載網(wǎng)絡(luò)的文章,小編覺得很優(yōu)秀,歡迎點擊閱讀原文,支持原創(chuàng)作者,如有侵權(quán),懇請聯(lián)系小編刪除。同時感謝您的閱讀,期待您的關(guān)注。

          瀏覽 86
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  免费无码做爱视频 | 超碰在线欧美 | 91乱伦网站 | 日韩欧美中文在线视频 | 91香蕉在线视频 |