Android中BroadcastReceiver主要用途有
发送通知,更新UI或者数据,应用程序间相互通信,监听系统状态(比如开机,网络等)
Android中BroadcasetReceiver的注册方式
manifest清单文件中的全局注册
按照生命周期,在Service或者Activity中使用代码注册
manifest的注册方式
1
2
3
4
5
6
|
<receiver android:name= "com.sample.test.MyBroadcastReciever" >
<intent-filter>
<action android:name= "com.sample.test.ACTION_DO_SOMETHING" ></action>
<action android:name= "android.intent.ACTION_WIFI_STATE_CHANGED" ></action>
</intent-filter>
</receiver>
|
使用代码注册
SampleActivity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
private MyReceiver receiver;
@Override public void onStart() {
super .onStart();
receiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction( "android.intent.action.MY_BROADCAST" );
registerReceiver(receiver, filter);
} @Override public void onStop(){
super .onStop();
unregisterReceiver(receiver);
} |
Android中发送广播的方式
普通广播:无论优先级大小,将发送给所有监听Action="com.test.sample.action"的广播,内容不可被修改,无传递性。
1
2
|
Intent intent = new Intent( "com.test.sample.action" );
sendBroadcast(intent); |
异步(黏性)广播: 当处理完之后的Intent ,依然存在,这时候registerReceiver(BroadcastReceiver, IntentFilter) 还能收到他的值,直到你把它去掉 , 无传递性 , 无法终止(abort())广播。
发这个广播需要权限<uses-permission android:name="android.permission.BROADCAST_STICKY" />
去掉是用这个方法removeStickyBroadcast(intent); 但别忘了在执行这个方法的应用里面 AndroidManifest.xml 同样要加上面的权限;
1
2
|
sendStickyOrderedBroadcast(intent, resultReceiver, scheduler, initialCode, initialData, initialExtras)
|
有序广播:
按照接收者的优先级顺序接收广播 , 优先级别在 intent-filter 中的 priority 中声明 ,-1000 到1000 之间 ,值越大 优先级越高 。可以终止广播意图的继续传播 , 接收者可以篡改内容,具有传递性。
1
|
sendBroadcast(intent); |
Android中的BroadcastReceiver可以用来发送信息到另一个广播,这种方式可实现程序或者进程间的通行。
上面回顾了一下Android的广播用例,总体来说安全性都不太好,因此只适用于安全性较低的数据传递,或者页面更新。
在android-support-v4.jar中引入了LocalBroadcastManager,称为局部通知管理器,这种通知的好处是安全性高,效率也高,适合局部通信,可以用来代替Handler更新UI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
public class LocalServiceBroadcasterActivity extends Activity {
static final String ACTION_STARTED = "com.example.android.supportv4.STARTED" ;
static final String ACTION_UPDATE = "com.example.android.supportv4.UPDATE" ;
static final String ACTION_STOPPED = "com.example.android.supportv4.STOPPED" ;
LocalBroadcastManager mLocalBroadcastManager;
BroadcastReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView callbackData = (TextView) findViewById(R.id.callback);
callbackData.setText( "No broadcast received yet" );
mLocalBroadcastManager = LocalBroadcastManager.getInstance( this );
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_STARTED);
filter.addAction(ACTION_UPDATE);
filter.addAction(ACTION_STOPPED);
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_STARTED)) {
callbackData.setText( "STARTED" );
} else if (intent.getAction().equals(ACTION_UPDATE)) {
callbackData.setText( "Got update: " + intent.getIntExtra( "value" , 0 ));
} else if (intent.getAction().equals(ACTION_STOPPED)) {
callbackData.setText( "STOPPED" );
}
}
};
mLocalBroadcastManager.registerReceiver(mReceiver, filter);
Button button = (Button) findViewById(R.id.start);
button.setOnClickListener(mStartListener);
button = (Button) findViewById(R.id.stop);
button.setOnClickListener(mStopListener);
}
@Override
protected void onDestroy() {
super .onDestroy();
mLocalBroadcastManager.unregisterReceiver(mReceiver);
}
private OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
startService( new Intent(LocalServiceBroadcasterActivity. this , LocalService. class ));
}
};
private OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
stopService( new Intent(LocalServiceBroadcasterActivity. this , LocalService. class ));
}
};
public static class LocalService extends Service {
LocalBroadcastManager mLocalBroadcastManager;
int mCurUpdate;
static final int MSG_UPDATE = 1 ;
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_UPDATE: {
mCurUpdate++;
Intent intent = new Intent(ACTION_UPDATE);
intent.putExtra( "value" , mCurUpdate);
mLocalBroadcastManager.sendBroadcast(intent);
Message nmsg = mHandler.obtainMessage(MSG_UPDATE);
mHandler.sendMessageDelayed(nmsg, 1000 );
}
break ;
default :
super .handleMessage(msg);
}
}
};
@Override
public void onCreate() {
super .onCreate();
mLocalBroadcastManager = LocalBroadcastManager.getInstance( this );
}
public int onStartCommand(Intent intent, int flags, int startId) {
// Tell any local interested parties about the start.
mLocalBroadcastManager.sendBroadcast( new Intent(ACTION_STARTED));
// Prepare to do update reports.
mHandler.removeMessages(MSG_UPDATE);
Message msg = mHandler.obtainMessage(MSG_UPDATE);
mHandler.sendMessageDelayed(msg, 1000 );
return ServiceCompat.START_STICKY;
}
@Override
public void onDestroy() {
super .onDestroy();
// Tell any local interested parties about the stop.
mLocalBroadcastManager.sendBroadcast( new Intent(ACTION_STOPPED));
// Stop doing updates.
mHandler.removeMessages(MSG_UPDATE);
}
@Override
public IBinder onBind(Intent intent) {
return null ;
}
}
} |
摘自:http://my.oschina.net/ososchina/blog/340339