Android開發(fā)之BroadcastReceiver用法實(shí)例分析
本文實(shí)例講述了Android開發(fā)中BroadcastReceiver用法。分享給大家供大家參考。具體分析如下:
在Android系統(tǒng)中,廣播(Broadcast)是在組件之間傳播數(shù)據(jù)(Intent)的一種機(jī)制。
Braodcast Receiver顧名思義就是廣播接收器,它和事件處理機(jī)制類似,但是事件處理機(jī)制是程序組件級(jí)別的(比如:按鈕的單擊事件),而廣播事件處理機(jī)制是系統(tǒng)級(jí)別的。我們可以用Intent來啟動(dòng)一個(gè)組件,也可以用sendBroadcast()方法發(fā)起一個(gè)系統(tǒng)級(jí)別的事件廣播來傳遞消息。我們同樣可以在自己的應(yīng)用程序中實(shí)現(xiàn)Broadcast Receiver來監(jiān)聽和響應(yīng)廣播的Intent。
事件的廣播通過創(chuàng)建Intent對(duì)象并調(diào)用sendBroadcast()方法將廣播發(fā)出。事件的接受是通過定義一個(gè)繼承BroadcastReceiver的類來實(shí)現(xiàn)的,繼承該類后覆蓋其onReceive()方法,在該方法中響應(yīng)事件。
下面是android系統(tǒng)中定義了很多標(biāo)準(zhǔn)的Broadcast Action來響應(yīng)系統(tǒng)的廣播事件。
①ACTION_TIME_CHANGED(時(shí)間改變時(shí)觸發(fā))
②ACTION_BOOT_COMPLETED(系統(tǒng)啟動(dòng)完成后觸發(fā))--比如有些程序開機(jī)后啟動(dòng)就是用這種方式來實(shí)現(xiàn)的
③ACTION_PACKAGE_ADDED(添加包時(shí)觸發(fā))
④ACTION_BATTERY_CHANGED(電量低時(shí)觸發(fā))
下面看一個(gè)例子:
我們在一個(gè)按鈕上綁定一個(gè)事件,事件通過發(fā)送一個(gè)廣播來觸發(fā)logcat打出一個(gè)log。
先看manifest文件。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.broadcast.BroadcastReceiverActivity" android:label="@string/app_name_bc" > <intent-filter> <action android:name="android.intent.action.MAIN" > </action> <category android:name="android.intent.category.LAUNCHER" > </category> </intent-filter> </activity> <receiver android:name="com.example.broadcast.HelloBroadReciever" > <intent-filter> <action android:name="comz.test.printlog" > </action> </intent-filter> </receiver> </application> </manifest>
上面聲明了一個(gè)receiver。接收名字是comz.test.printlog的消息。
看activity:
package com.example.broadcast; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import com.example.test.R; public class BroadcastReceiverActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button b1 = (Button) findViewById(R.id.broadcastBtn); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("mason", "here"); // 定義一個(gè)intent Intent intent = new Intent().setAction("comz.test.printlog") .putExtra("info", "here is your info."); // 廣播出去 sendBroadcast(intent); } }); } }
在這段代碼中,定義一個(gè)intent并發(fā)送廣播出去。
看BroadReceiver的代碼:
package com.example.broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class HelloBroadReciever extends BroadcastReceiver { // 如果接收的事件發(fā)生 @Override public void onReceive(Context context, Intent intent) { Log.e("mason", "on receive"); if (intent.getAction().equals("comz.test.printlog")) { Log.e("mason", intent.getStringExtra("info")); } } }
這是BroadcastReceiver的代碼。
在接收到消息之后,如果消息是comz.test.printlog,則打印消息。
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- 深入Android中BroadcastReceiver的兩種注冊方式(靜態(tài)和動(dòng)態(tài))詳解
- Android中BroadcastReceiver(異步接收廣播Intent)的使用
- Android提高之BroadcastReceiver實(shí)例詳解
- Android編程四大組件之BroadcastReceiver(廣播接收者)用法實(shí)例
- Android查看電池電量的方法(基于BroadcastReceiver)
- Android BroadcastReceiver廣播注冊方式總結(jié)
- 詳解Android中BroadCastReceiver組件
- Android采取BroadcastReceiver方式自動(dòng)獲取驗(yàn)證碼
- Android BroadcastReceiver常見監(jiān)聽整理
- Android BroadcastReceiver實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)實(shí)時(shí)監(jiān)聽
相關(guān)文章
Android SQLite數(shù)據(jù)庫基本操作方法
本篇文章主要介紹了Android SQLite數(shù)據(jù)庫基本操作方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02Android入門之RelativeLayout、FrameLayout用法分析
這篇文章主要介紹了Android入門之RelativeLayout、FrameLayout用法分析,需要的朋友可以參考下2014-08-08Android總結(jié)之WebView與Javascript交互(互相調(diào)用)
本篇文章主要介紹了WebView與Javascript進(jìn)行數(shù)據(jù)交互,詳解的講訴了WebView與Javascript進(jìn)行數(shù)據(jù)交互的方法,有興趣的可以了解一下。2016-11-11Android編程實(shí)現(xiàn)獲得內(nèi)存剩余大小與總大小的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)獲得內(nèi)存剩余大小與總大小的方法,涉及Android基于ActivityManager實(shí)現(xiàn)內(nèi)存信息的操作技巧,需要的朋友可以參考下2015-12-12Android AlertDialog自定義樣式實(shí)現(xiàn)代碼
這篇文章主要介紹了Android AlertDialog自定義樣式實(shí)現(xiàn)代碼的相關(guān)資料,這里提供了實(shí)例代碼,一個(gè)簡單示例,需要的朋友可以參考下2016-12-12