Broadcast是一種普遍運用的在應用程序之間傳輸信息的機制。android
1、下面是幾個經常使用的系統廣播app
//重啓設備時的廣播ide
Intent.ACTION_REBOOT;ui
//屏幕被關閉以後的廣播this
Intent.ACTION_SCREEN_OFF;spa
//屏幕被打開以後的廣播component
Intent.ACTION_SCREEN_ON;orm
//在系統啓動完成後,這個動做被廣播一次(只有一次)。xml
Intent.ACTION_BOOT_COMPLETED;生命週期
//識別用戶進入home界面
Intent.ACTION_USER_PRESENT;
2、BroadcastReceiver整體上能夠分爲兩種註冊類型:靜態註冊和動態註冊:
一、AndroidManifest.xml文件中靜態註冊:
<receiver android:name="******.MyBroadcastReceiver"
android:exported=["true" | "false"] >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
android:exported:此broadcastReceiver可否接收其餘App的發出的廣播,這個屬性默認是由receiver中有 無intent-filter決定的,若是有intent-filter,默認值爲true,不然爲false。
二、代碼中動態註冊:
MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(android.intent.action.BOOT_COMPLETED);
registerReceiver(myBroadcastReceiver , intentFilter);
3、廣播接收的生命週期(10s):
在Android中,每次廣播消息到來時都會建立BroadcastReceiver實例並執行onReceive() 方法,onReceive() 方法執行完後,BroadcastReceiver 的實例就會被銷燬,當onReceive() 方法在10秒內沒有執行完畢,Android會認爲該程序無響應,會彈出 ANR的對話框。因此在onReceive() 裏不能作一些比較耗時的操做,若是須要完成一項比較耗時的工做 , 應該經過發送 Intent 給 Service, 由 Service 來完成 。
4、程序被強制中止沒法接收廣播:
官方說明:
Launch controls on stopped applications
Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications.
Note that an application's stopped state is not the same as an Activity's stopped state. The system manages those two stopped states separately.
The platform defines two new intent flags that let a sender specify whether the Intent should be allowed to activate components in stopped application.
FLAG_INCLUDE_STOPPED_PACKAGES — Include intent filters of stopped applications in the list of potential targets to resolve against.
FLAG_EXCLUDE_STOPPED_PACKAGES — Exclude intent filters of stopped applications from the list of potential targets.
When neither or both of these flags is defined in an intent, the default behavior is to include filters of stopped applications in the list of potential targets.
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. A background service or application can override this behavior by adding theFLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications.
Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications).
依據上面的說明自定義廣播只須要在發送廣播時設置一個標識就能夠解決在程序被強制中止時仍然能夠接收廣播:
Intent intent = new Intent();
intent.setAction("******");
if (android.os.Build.VERSION.SDK_INT >= 12) {
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);//3.1之後的版本須要設置
}
sendBroadcast(intent);
而對於系統廣播,目前尚未找到解決辦法。