在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ Android廣播接收器
Android 應用組件
使用布局文件自定義Android組件
Android通知
Android主題示例
Android JetPlayer實例
Android MediaPlayer(多媒體播放)
Android AbsoluteLayout
Android FrameLayout
Android Gestures/手勢
Android AutoCompleteTextView(自動完成)實例
Android 資源組織和訪問
Android ListView
Android GridView
Android數(shù)據(jù)備份
Android撥打電話
Android發(fā)送短信/SMS
Android ProgressDialog
SimpleCursorAdapter
Android發(fā)送電子郵件
Android Activity
Android TextView
Android事件處理
Android TableLayout
Android加載Spinner
Android內(nèi)容提供者
Android自定義字體
Android Service
Android CheckBox
Android Intent過濾器
Android LinearLayout
Android登錄實例
Android RadioButton
Android樣式和主題
Android自定義組件及屬性
Android UI控件
Android Animation(動畫)實例
Android Camera(攝像頭)
Android ToggleButton
Android Clipboard(復制/剪貼板)
Android音頻捕獲(錄音)
發(fā)布Android應用
Android Alertdialog(警告對話框)
Android圖片效果
Android內(nèi)部存儲
Android基于位置服務
Android RadioGroup
Android AutoCompleteTextView
Android Bluetooth(藍牙)實例
Android RelativeLayout
Android最佳實踐
Android本地化
Android自定義組件
Android教程
Android 架構
Android UI布局
Android Button
Android Hello World示例
Android音頻管理器實例
ArrayAdapter
Android拖放
Android碎片/片段
Android圖片切換
Android JSON解析器
Android開發(fā)環(huán)境搭建
Android Spinner
Android樣式示例
使用活動代碼自定義Android組件
Android ImageButton
Android EditText
Android廣播接收器

Android廣播接收器

廣播接收器(Broadcast)簡單地從其他應用程序或系統(tǒng)響應廣播消息。這些消息有時稱為事件或意圖。例如,應用程序也可以發(fā)起廣播,以讓其他應用程序知道某些數(shù)據(jù)已經(jīng)被下載到設備上,可供它們使用。廣播接收器會攔截此通信,并會采取適當操作(動作)。

以下兩個重要的步驟,在使用廣播接收器工作系統(tǒng)及廣播意圖:

  • 創(chuàng)建廣播接收器

  • 注冊廣播接收器

還有一個附加的步驟,要實現(xiàn)自定義的意圖,那么將必須創(chuàng)建并廣播意圖。

創(chuàng)建廣播接收器

實現(xiàn)廣播接收機BroadcastReceiver類的一個子類并重寫 onReceive()方法,其中每個收到消息作為一個 Intent 對象參數(shù)。

public class MyReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
   }

}

注冊廣播接收器

應用程序偵聽特定的廣播意圖是通過在 AndroidManifest.xml 文件中注冊一個廣播接收器。寄存器 MyReceiver 系統(tǒng)生成事件 ACTION_BOOT_COMPLETED,在Android系統(tǒng)完成了啟動過程后,這是由系統(tǒ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>

當 Android 設備啟動,它會被截獲 BroadcastReceiverMyReceiverand 內(nèi)實現(xiàn)邏輯,首先 onReceive() 將被執(zhí)行。

有幾個系統(tǒng)產(chǎn)生的事件定義在最后意圖類的靜態(tài)字段。下表列出了一些重要的系統(tǒng)事件。

事件常量 描述
android.intent.action.BATTERY_CHANGED 持久廣播含充電狀態(tài),級別,以及其他相關的電池信息。
android.intent.action.BATTERY_LOW 顯示設備的電池電量低。
android.intent.action.BATTERY_OKAY 指示電池正在低點后但沒有問題。
android.intent.action.BOOT_COMPLETED 一次播出后,系統(tǒng)已完成啟動。
android.intent.action.BUG_REPORT 顯示活動報告的錯誤。
android.intent.action.CALL 執(zhí)行呼叫由數(shù)據(jù)指定某人。
android.intent.action.CALL_BUTTON 用戶按下“呼叫”按鈕進入撥號器或其他適當?shù)挠脩艚缑姘l(fā)出呼叫。
android.intent.action.DATE_CHANGED 日期改變。
android.intent.action.REBOOT 有設備重啟。

廣播定制意圖

如果希望應用程序本身生成并發(fā)送自定義意圖,那么必須使用sendBroadcast()方法里面活動類來創(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也可以以注冊類似的方式,因為我們產(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>

示例

這個例子將解釋如何創(chuàng)建BroadcastReceiver 攔截自定義意圖。熟悉自定義意圖后,就可以編寫應用程序來攔截系統(tǒng)生成的意圖?,F(xiàn)在按照下面的步驟來修改前面創(chuàng)建的Hello World范例中 Android 應用程序:

步驟 描述
1 使用Eclipse IDE創(chuàng)建Android應用程序,并將其命名為HelloWorld在包com.example.helloworld下,類似Hello World示例章節(jié)中一樣。
2 修改主要活動文件MainActivity.java添加broadcastIntent()方法。
3 在包com.example.helloworld下創(chuàng)建一個新的Java文件 MyReceiver.java,并定義一個BroadcastReceiver。
4 應用程序可以處理一個或多個自定義和系統(tǒng)的意圖不受任何限制。要攔截每一個意圖,必須使用 <receiver.../>標簽并注冊在AndroidManifest.xml文件中。
5 修改 res/layout/activity_main.xml 文件的默認內(nèi)容包括:一個按鈕廣播意圖。
6 定義常量 broadcast_inte 在 ntres/values/strings.xml文件中
7 運行該應用程序啟動Android模擬器并驗證應用程序所做的修改結果。

以下是修改主要活動文件 src/com.example.helloworld/MainActivity.java 后的內(nèi)容。這個文件包括每個生命周期方法。這里添加了 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樣式示例