標籤:stat 電量 cas init rmi html 隊列 main one
通過第一篇的解說,我們已經看到了怎樣使用Intent來啟動新的應用程式組件,可是實際上他們也能夠使用sendBroadcast方法來在組件間匿名的廣播訊息。
作為一個系統層級的訊息傳遞機制,Intent能夠在進程之間發送結構化的訊息。
因此,通過實現Broadcast Receiver來監聽和響應應用程式內的這些Broadcast Intent。
通過使用Intent來廣播一個事件,能夠在不改動原始的應用程式的情況下。讓我們開發人員對事件做出反應。Android大量使用了Broadcast Receiver來廣播系統事件,如網路連接和簡訊通知等。
1.使用Intent來廣播事件
在應用程式組件中,能夠構建希望廣播的Intent,然後使用sendBroadcast方法來發送它。
可以對Intent的動作、資料和分類進行設定,從而使Broadcast Receiver可以精確的確定他們的需求。
在這樣的方案中,Intent動作字串可以用來標識要廣播的事件,所以他應該是可以標識事件的唯一的字串。習慣上。動作字串使用與Java包名同樣的構建方式。例如以下:
<span style="font-size:14px;">public static final String MY_INTENT_ACTION = "com.happy.demo.test";</span>
假設希望在Intent中包括資料。那麼能夠使用Intent的data屬性指定一個URI,也可能夠包括extras來加入額外的基本值。
2.使用Broadcast Receiver來監聽廣播
Broadcast Receiver能夠用來監聽Broadcast Intent,可是須要對其進行注冊。既能夠使用代碼方式來注冊。也能夠在應用程式的manifest.xml檔案裡進行注冊。無論採用哪種方式,都須要使用一個Intent Filter來指定他要監聽哪些Intent和資料。
對於包括在manifest.xml檔案裡的廣播接收者 。在Intent被廣播出去的時候。應用程式不一定非要處於執行狀態才幹接收。他們會被自己主動的啟動。
建立一個新的Broadcast Receiver,須要擴充Broadcast Receiver類並重寫onReceive方法來進行自己的處理。比方:
<span style="font-size:14px;">package com.happy.demo;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;public class MyBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stub}}</span> 當接收到一個與在注冊接收器時使用的IntentFilter相匹配的Broadcast Intent的時候,就會運行onReceive方法。onReceive處理常式必需要在5秒鐘以內完畢。否則會顯示Force Close對話方塊。
普通情況下,Broadcast Receiver將會更新內容、啟動Service、更新Activity UI或者使用Notification Manager來通知使用者。
3.在代碼中注冊Broadcast Receiver
影響特定Activity的UI的Boradcast Receiver通常在代碼中注冊,在代碼中注冊的接收器僅僅會在包括它的應用程式組件執行時對應Broadcast Intent。
在接收器用來更新一個Activity中的UI元素時,一般在onResume中注冊接收器,在onPause中登出接收器。代碼:
<span style="font-size:14px;">@Overrideprotected void onResume() {super.onResume();registerReceiver(receiver, filter);}@Overrideprotected void onPause() {super.onPause();unregisterReceiver(receiver);}</span>4.在應用程式的manifets.xml檔案裡進行注冊
要在應用程式的mainfets中包括一個Broadreceiver。能夠在application節點中加入一個receiver標籤。以及指定要注冊的Broadcast Receiver的類名。接收器節點中須要包括一個intent-filter標籤來指定要監聽的動作字串。例如以下:
<span style="font-size:14px;"><receiver android:name="com.happy.demo.MyBroadcastReceiver" > <intent-filter> <action android:name="com.happy.demo.one" /> </intent-filter> </receiver></span>
5.廣播有序的Intent
當Broadcast Receiver接收Intent的順序十分重要時。特別是當須要將來接收器可以影響將來的接收器收到的Broadcast Intent時。可以使用sendOrderedBroadcast方法。例如以下:
<span style="font-size:14px;">String requiredPermission = "com.happy.demo.one";sendOrderedBroadcast(intent, requiredPermission);</span>
使用這種方法時。Intent將會依照優先順序被傳遞給全部具有合適許可權的已注冊的接收器。能夠在manifest.xml中使用android:priority屬性指定其許可權,值越大。代表優先順序越高。
<span style="font-size:14px;"> <receiver android:name="com.happy.demo.MyBroadcastReceiver" android:permission="android.permission.ACCESS_CHECKIN_PROPERTIES" > <intent-filter android:priority="1000" > <action android:name="com.happy.demo.one" /> </intent-filter> </receiver></span>
發送有序廣播的一種常見範例是廣播想要收到其結果資料的Intent。使用sendOrderedBroadcast方法時,能夠指定一個將放到接收器隊列末尾的接收器,而從保證當Broadacst Receiver已被已注冊的有序Broadcast Receiver處理和改動後,它也能接收到該Broadcast Intent。
在這樣的情況下。對於那些在返回給最後一個接收器之前可能被不論什麼收到廣播的接收器改動的Intent結果、資料和extra,為他們指定預設值通常非常有協助。例如以下:
<span style="font-size:14px;">sendOrderedBroadcast(intent, receiverPermission, resultReceiver, scheduler, initialCode, initialData, initialExtras);</span>
6.廣播Stick Intent
Stity Intent是Broadcast Intent的實用變體,能夠儲存他們最後一次廣播的值,而且當有一個新的接收器被注冊為接收該廣播時,他們會把這些值作為Intent返回。
當調用registerReceiver來指定一個匹配的Sticy Broadcast Intent的Intent Filter時,傳回值將是最後一個Intent廣播,比如電池電量變化的廣播
7.Local Boradcast Manager
局部廣播管理器包括在Android Support Library中。使用者簡化注冊Broadcast Inten以及在應程式內的組件之間發送Broadcast Intent的工作。由於局部廣播的範圍要小,所以使用它比發送全域廣播更加高效,並且也確保了應用程式外部的不論什麼組件都收不到我們的廣播Intent,所以也是安全的。與之同樣。其它應用程式也不能向我們的接收器發送廣播。避免了這些接收器成為安全性漏洞。
擷取Local Broadcast Manager的方法例如以下:
<span style="font-size:14px;">LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getApplicationContext());</span>
注冊一個局部廣播接收者和注冊全域的一樣,代碼例如以下:
<span style="font-size:14px;">lbm.registerReceiver(receiver, intentFilter);</span>
要發送一個局部Broadcast Intent,能夠使用Local Boradcast Manager的sendBroadcast方法,並傳入要廣播的Intent。例如以下:
<span style="font-size:14px;">lbm.sendBroadcast(intent);</span>
8.Pending Intent簡單介紹
PendingIntent類提供了一種建立可由其它應用程式在稍晚的時間觸發的Intent的機制。
PendingIntent通經常使用於封裝在響應將來的事件時觸發的Intent,比如單機Widget或者Notification。
PendingIntent類提供了構建PendingIntent的靜態方法,以便啟動Activity Service或者廣播Intent。
<span style="font-size:14px;">PendingIntent.getActivity(context, requestCode, intent, flags);PendingIntent.getService(context, requestCode, intent, flags);PendingIntent.getBroadcast(context, requestCode, intent, flags);</span>
PendingIntent類包括了一些靜態常量,他們能夠用於指定標誌,以更新或取消與指定動作匹配的現有PendingIntent,也能夠用於指定該Intent是否僅僅觸發一次。
Android中Intent具體解釋(二)之使用Intent廣播事件及Broadcast Receiver簡單介紹