參考: Android入門:廣播發送者與廣播接收者 - xiazdong - CSDN博客http://blog.csdn.net/xiazdong/article/details/7768807 一、廣播發送者&廣播接收者介紹 1.廣播接收者 廣播接收者簡單地說就是接收廣播意圖的Java類,此Ja ...
參考:
Android入門:廣播發送者與廣播接收者 - xiazdong - CSDN博客
http://blog.csdn.net/xiazdong/article/details/7768807
一、廣播發送者&廣播接收者介紹
1.廣播接收者
廣播接收者簡單地說就是接收廣播意圖的Java類,此Java類繼承BroadcastReceiver類,重寫:
public void onReceive(Context context,Intent intent),其中intent可以獲得傳遞的數據;
廣播意圖就是通過Context.sendBroadcast(Intent intent)或Context.sendOrderedBroadcast(Intent intent)發送的意圖,通過這個語句,能夠廣播給所有滿足條件的組件,比如intent設置了action="com.xiazdong",則所有在AndroidManifest.xml中設置過<action android:name="com.xiazdong"/>的廣播接收者都能夠接收到廣播;
註:onReceive方法必須在10秒內完成,如果沒有完成,則拋“Application No Response”當廣播接收者onReceive方法需要執行很長時間時,最好將此耗時工作通過Intent發送給Service,由Service完成,並且不能使用子線程解決,因為BroadcastReceiver是接收到廣播後才創建的,並且生命周期很短,因此子線程可能在沒有執行完就已經被殺死了。
[java] view plain copy
- public void onReceive(Context context,Intent intent){
- Intent intent = new Intent(context,XxxService.class);
- context.startService(intent);
- }
2.廣播發送者
通常廣播發送方就是調用Context.sendBroadcast()的程式,而廣播接收者就是繼承BroadcastReceiver的程式;
通常廣播發送方都是通過隱式意圖,這樣才能發送給多人;
廣播發送方分為普通廣播和有序廣播;
同步廣播:發送方發出後,幾乎同時到達多個廣播接收者處,某個接收者不能接收到廣播後進行一番處理後傳給下一個接收者,並且無法終止廣播繼續傳播;Context.sendBroadcast(intent);
有序廣播:廣播接收者需要提前設置優先順序,優先順序高的先接收到廣播,優先順序數值為-1000~1000,在AndroidManifest.xml的<intent-filter android:priority="xxx">設置;比如存在3個廣播接收者A、B、C,優先順序A>B>C,因此A最先收到廣播,當A收到廣播後,可以向廣播中添加一些數據給下一個接收者(intent.putExtra()),或者終止廣播(abortBroadcast());Context.sendOrderedBroadcast(intent);
二、廣播接收者核心代碼
同步廣播發送方核心代碼:
[java] view plain copy
- Intent intent = new Intent();
- intent.setAction("...");
- Context.sendBroadcast(intent);
有序廣播發送方核心代碼:
[java] view plain copy
- Intent intent = new Intent();
- intent.setAction("...");
- Context.sendOrderedBroadcast(intent,null);
廣播接收者核心代碼:
[java] view plain copy
- public class Receiver extends BroadcastReceiver{
- public void onReceive(Context context, Intent intent) {
- Bundle bundle = intent.getExtras();
- ...
- }
- }
AndroidManifest.xml
[html] view plain copy
- <application>
- <receiver android:name=".Receiver">
- <intent-filter android:priority="1000">
- <action android:name="com.xiazdong"/>
- </intent-filter>
- </receiver>
- </application>
三、廣播實例
1.同步廣播實例
場景說明:
(1)廣播發送者:
[java] view plain copy
- package com.xiazdong.broadcastsender;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private Button button;
- private OnClickListener listener = new OnClickListener(){
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setAction("com.xiazdong");
- intent.putExtra("name", "xiazdong");
- MainActivity.this.sendBroadcast(intent);
- Toast.makeText(getApplicationContext(), "發送廣播成功", Toast.LENGTH_SHORT).show();
- }
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button)this.findViewById(R.id.button);
- button.setOnClickListener(listener);
- }
- }
(2)廣播接收者
[java] view plain copy
- package com.xiazdong.broadcastreceiver1;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- public class Receiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- String name = intent.getExtras().getString("name");
- Log.i("Recevier1", "接收到:"+name);
- }
- }
AndroidManifest.xml
[java] view plain copy
- <receiver android:name=".Receiver">
- <intent-filter>
- <action android:name="com.xiazdong"/>
- </intent-filter>
- </receiver>
結果:
2.有序廣播實例
場景說明:
(1)廣播發送者
[java] view plain copy
- package com.xiazdong.broadcastsender;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private Button button;
- private OnClickListener listener = new OnClickListener(){
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setAction("com.xiazdong");
- intent.putExtra("name", "xiazdong");
- MainActivity.this.sendOrderedBroadcast(intent, null); //有序廣播發送
- Toast.makeText(getApplicationContext(), "發送廣播成功", Toast.LENGTH_SHORT).show();
- }
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button)this.findViewById(R.id.button);
- button.setOnClickListener(listener);
- }
- }
(2)廣播接收者
Receiver1
[java] view plain copy
- package com.xiazdong.broadcastreceiver1;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- public class Receiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- String name = intent.getExtras().getString("name");
- Log.i("Recevier1", "接收到:"+name);
- abortBroadcast(); //Receiver1接收到廣播後中斷廣播
- }
- }
AndroidManifest.xml
[html] view plain copy
- <receiver android:name=".Receiver">
- <intent-filter android:priority="1000"> <!-- 設置最高優先順序 -->
- <action android:name="com.xiazdong"/>
- </intent-filter>
- </receiver>