Android中BroadcastReceiver组件详解
Android系統(tǒng)的4個組件終于還剩一種組件了BroadcastReceiver,這個組件是全局監(jiān)聽器,可以監(jiān)聽系統(tǒng)全局的廣播消息,可以方便的實現(xiàn)系統(tǒng)中不同組件之間的通信
BroadcastReceiver有自己的進程,系統(tǒng)級監(jiān)聽器,只要存在與之匹配的Intent被廣播出來,BroadcastReceiver就會被激發(fā)
要創(chuàng)建自己的BroadcastReceiver對象,我們需要繼承android.content.BroadcastReceiver,并實現(xiàn)其onReceive方法
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent){Toast.makeText(context,"接收到的Intent的Action為:" + intent.getAction() + "\n消息內(nèi)容是:" + intent.getStringExtra("msg"), Toast.LENGTH_LONG).show();} }Manifest.xml清單文件配置的receiver
<receiver android:name=".MyReceiver"><intent-filter><action android:name="org.crazyit.action.CRAZY_BROADCAST" /></intent-filter> </receiver>就是說無論哪個組件中,intent的Action是"org.crazyit.action.CRAZY_BROADCAST" 并使用使用sendBroadcast(intent)發(fā)出廣播,那么MyReceiver就會被啟動
public class BroadcastMain extends Activity {Button send;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// 獲取程序界面中的按鈕send = (Button) findViewById(R.id.send);send.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){// 創(chuàng)建Intent對象Intent intent = new Intent();// 設(shè)置Intent的Action屬性intent.setAction("org.crazyit.action.CRAZY_BROADCAST");intent.putExtra("msg", "簡單的消息");// 發(fā)送廣播sendBroadcast(intent);}});} }
注冊Receiver有兩種方法:
靜態(tài)注冊
靜態(tài)注冊是在AndroidManifest.xml文件中配置的,我們就來為MyReceiver注冊一個廣播地址:
<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="android.intent.action.MY_BROADCAST"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver>
配置了以上信息之后,只要是android.intent.action.MY_BROADCAST這個地址的廣播,MyReceiver都能夠接收的到。注意,這種方式的注冊是常駐型的,也就是說當應(yīng)用關(guān)閉后,如果有廣播信息傳來,MyReceiver也會被系統(tǒng)調(diào)用而自動運行。
動態(tài)注冊
動態(tài)注冊需要在代碼中動態(tài)的指定廣播地址并注冊,通常我們是在Activity或Service注冊一個廣播,下面我們就來看一下注冊的代碼:
MyReceiver receiver = new MyReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.MY_BROADCAST"); registerReceiver(receiver, filter); //注冊<pre name="code" class="java">receiver 和filter 普通廣播
普通廣播對于多個接收者來說是完全異步的,通常每個接收者都無需等待即可以接收到廣播,接收者相互之間不會有影響。對于這種廣播,接收者無法終止廣播,即無法阻止其他接收者的接收動作。上面的例子就是發(fā)送的普通廣播
有序廣播
有序廣播比較特殊,它每次只發(fā)送到優(yōu)先級較高的接收者那里,然后由優(yōu)先級高的接受者再傳播到優(yōu)先級低的接收者那里,優(yōu)先級高的接收者有能力終止這個廣播。
例如:優(yōu)先級A>B>C,Broadcast先傳給A,再傳給B,在傳給C。優(yōu)先級別聲明<Intent-filter>元素的android:priority中,數(shù)越大級別越高,取值范圍在-1000~1000
優(yōu)先收到Broadcast的接受者可以通過setResultExtras(Bundle)方法將處理結(jié)果存入Broadcast中,然后傳給下一個接受者,通過Bundle bunde=getResultExtras(true)柯獲得上一個接受者存入的數(shù)據(jù)
public class SortedBroadcast extends Activity {Button send;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// 獲取程序中的send按鈕send = (Button) findViewById(R.id.send);send.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){// 創(chuàng)建Intent對象Intent intent = new Intent();intent.setAction("org.crazyit.action.CRAZY_BROADCAST");intent.putExtra("msg", "簡單的消息");// 發(fā)送有序廣播sendOrderedBroadcast(intent, null);}});} } MyReceiver.java
public class MyReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent){Toast.makeText(context, "接收到的Intent的Action為:" + intent.getAction() + "\n消息內(nèi)容是:"+ intent.getStringExtra("msg"), Toast.LENGTH_LONG).show();// 創(chuàng)建一個Bundle對象,并存入數(shù)據(jù)Bundle bundle = new Bundle();bundle.putString("first", "第一個BroadcastReceiver存入的消息");// 將bundle放入結(jié)果中setResultExtras(bundle);// 取消Broadcast的繼續(xù)傳播// abortBroadcast(); //①} } MyReceiver2.java public class MyReceiver2 extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent){Bundle bundle = getResultExtras(true);// 解析前一個BroadcastReceiver所存入的key為first的消息String first = bundle.getString("first");Toast.makeText(context, "第一個Broadcast存入的消息為:" + first, Toast.LENGTH_LONG).show();} } 清單文件
<receiver android:name=".MyReceiver"><intent-filter android:priority="20"><action android:name="org.crazyit.action.CRAZY_BROADCAST" /></intent-filter> </receiver><receiver android:name=".MyReceiver2"><intent-filter android:priority="0"><action android:name="org.crazyit.action.CRAZY_BROADCAST" /></intent-filter> </receiver>
總結(jié)
以上是生活随笔為你收集整理的Android中BroadcastReceiver组件详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用IntentService在Serv
- 下一篇: Android中接收系统广播消息