廣播接收器(Broadcast)簡單地從其他應(yīng)用程序或系統(tǒng)響應(yīng)廣播消息。這些消息有時(shí)稱為事件或意圖。例如,應(yīng)用程序也可以發(fā)起廣播,以讓其他應(yīng)用程序知道某些數(shù)據(jù)已經(jīng)被下載到設(shè)備上,可供它們使用。廣播接收器會(huì)攔截此通信,并會(huì)采取適當(dāng)操作(動(dòng)作)。
以下兩個(gè)重要的步驟,在使用廣播接收器工作系統(tǒng)及廣播意圖:
創(chuàng)建廣播接收器
注冊廣播接收器
還有一個(gè)附加的步驟,要實(shí)現(xiàn)自定義的意圖,那么將必須創(chuàng)建并廣播意圖。
實(shí)現(xiàn)廣播接收機(jī)BroadcastReceiver類的一個(gè)子類并重寫 onReceive()方法,其中每個(gè)收到消息作為一個(gè) Intent 對象參數(shù)。
public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show(); } }
應(yīng)用程序偵聽特定的廣播意圖是通過在 AndroidManifest.xml 文件中注冊一個(gè)廣播接收器。寄存器 MyReceiver 系統(tǒng)生成事件 ACTION_BOOT_COMPLETED,在Android系統(tǒng)完成了啟動(dòng)過程后,這是由系統(tǒng)啟動(dòng)執(zhí)行的。
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name="MyReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"> </action> </intent-filter> </receiver> </application>
當(dāng) Android 設(shè)備啟動(dòng),它會(huì)被截獲 BroadcastReceiver 的 MyReceiverand 內(nèi)實(shí)現(xiàn)邏輯,首先 onReceive() 將被執(zhí)行。
有幾個(gè)系統(tǒng)產(chǎn)生的事件定義在最后意圖類的靜態(tài)字段。下表列出了一些重要的系統(tǒng)事件。
| 事件常量 | 描述 |
|---|---|
| android.intent.action.BATTERY_CHANGED | 持久廣播含充電狀態(tài),級(jí)別,以及其他相關(guān)的電池信息。 |
| android.intent.action.BATTERY_LOW | 顯示設(shè)備的電池電量低。 |
| android.intent.action.BATTERY_OKAY | 指示電池正在低點(diǎn)后但沒有問題。 |
| android.intent.action.BOOT_COMPLETED | 一次播出后,系統(tǒng)已完成啟動(dòng)。 |
| android.intent.action.BUG_REPORT | 顯示活動(dòng)報(bào)告的錯(cuò)誤。 |
| android.intent.action.CALL | 執(zhí)行呼叫由數(shù)據(jù)指定某人。 |
| android.intent.action.CALL_BUTTON | 用戶按下“呼叫”按鈕進(jìn)入撥號(hào)器或其他適當(dāng)?shù)挠脩艚缑姘l(fā)出呼叫。 |
| android.intent.action.DATE_CHANGED | 日期改變。 |
| android.intent.action.REBOOT | 有設(shè)備重啟。 |
如果希望應(yīng)用程序本身生成并發(fā)送自定義意圖,那么必須使用sendBroadcast()方法里面活動(dòng)類來創(chuàng)建和發(fā)送這些的意圖。使用(意向)sendStickyBroadcast() 方法意圖是粘粘的,這意味著所發(fā)送的意圖保持周廣圍播出后完成。
public void broadcastIntent(View view) { Intent intent = new Intent(); intent.setAction("com.yiibai.CUSTOM_INTENT"); sendBroadcast(intent); }
意圖 com.yiibai.CUSTOM_INTENT也可以以注冊類似的方式,因?yàn)槲覀儺a(chǎn)生注冊系統(tǒng)的意圖。
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name="MyReceiver"> <intent-filter> <action android:name="com.yiibai.CUSTOM_INTENT"> </action> </intent-filter> </receiver> </application>
這個(gè)例子將解釋如何創(chuàng)建BroadcastReceiver 攔截自定義意圖。熟悉自定義意圖后,就可以編寫應(yīng)用程序來攔截系統(tǒng)生成的意圖?,F(xiàn)在按照下面的步驟來修改前面創(chuàng)建的Hello World范例中 Android 應(yīng)用程序:
| 步驟 | 描述 |
|---|---|
| 1 | 使用Eclipse IDE創(chuàng)建Android應(yīng)用程序,并將其命名為HelloWorld在包c(diǎn)om.example.helloworld下,類似Hello World示例章節(jié)中一樣。 |
| 2 | 修改主要活動(dòng)文件MainActivity.java添加broadcastIntent()方法。 |
| 3 | 在包c(diǎn)om.example.helloworld下創(chuàng)建一個(gè)新的Java文件 MyReceiver.java,并定義一個(gè)BroadcastReceiver。 |
| 4 | 應(yīng)用程序可以處理一個(gè)或多個(gè)自定義和系統(tǒng)的意圖不受任何限制。要攔截每一個(gè)意圖,必須使用 <receiver.../>標(biāo)簽并注冊在AndroidManifest.xml文件中。 |
| 5 | 修改 res/layout/activity_main.xml 文件的默認(rèn)內(nèi)容包括:一個(gè)按鈕廣播意圖。 |
| 6 | 定義常量 broadcast_inte 在 ntres/values/strings.xml文件中 |
| 7 | 運(yùn)行該應(yīng)用程序啟動(dòng)Android模擬器并驗(yàn)證應(yīng)用程序所做的修改結(jié)果。 |
以下是修改主要活動(dòng)文件 src/com.example.helloworld/MainActivity.java 后的內(nèi)容。這個(gè)文件包括每個(gè)生命周期方法。這里添加了 broadcastIntent() 方法來廣播自定義的意圖。
package com.example.helloworld; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.content.Intent; import android.view.View; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } // broadcast a custom intent. public void broadcastIntent(View view) { Intent intent = new Intent(); intent上一篇:Android加載Spinner下一篇:Android樣式示例