最近看了下关于广播方面的东西,加深了理解,也纠正了自己以前的不正确的看法。
关于广播有2中注册方式,一种是在代码中注册,也叫动态注册,另一种是在AndroidManifest里面注册,也叫静态注册。
不管在哪个里面注册,都需要发送广播sendBroadcast()。广播发送的是一个Intent,intent里面一般是action。
如果是在代码里面注册,一般在onResume()方法里面注册。
1 @Override
2 protectedvoid onResume() {
3 // TODO Auto-generated method stub
4
5 myReceiver =new MyReceiver();
6 IntentFilter filter =new IntentFilter();
7 //向过滤器中添加action
8 filter.addAction("com.android.shang");
9 //注册广播
10 registerReceiver(myReceiver, filter);
11
12 Intent intent =new Intent();
13 intent.setAction("com.android.xiang");
14 //发送广播
15 sendBroadcast(intent);
16 super.onResume();
17 }
2 protectedvoid onResume() {
3 // TODO Auto-generated method stub
4
5 myReceiver =new MyReceiver();
6 IntentFilter filter =new IntentFilter();
7 //向过滤器中添加action
8 filter.addAction("com.android.shang");
9 //注册广播
10 registerReceiver(myReceiver, filter);
11
12 Intent intent =new Intent();
13 intent.setAction("com.android.xiang");
14 //发送广播
15 sendBroadcast(intent);
16 super.onResume();
17 }
在onPause()方法里面要取消注册。
1 @Override
2 protectedvoid onPause() {
3 // TODO Auto-generated method stub
4 unregisterReceiver(myReceiver);
5 super.onPause();
6 }
2 protectedvoid onPause() {
3 // TODO Auto-generated method stub
4 unregisterReceiver(myReceiver);
5 super.onPause();
6 }
当然,也可以在xml文件中注册:
1 <receiver
2 android:name=".MyBrocastReceiverTest">
3 <intent-filter>
4 <action android:name="com.android.xiang"></action>
5 </intent-filter>
6 </receiver>
2 android:name=".MyBrocastReceiverTest">
3 <intent-filter>
4 <action android:name="com.android.xiang"></action>
5 </intent-filter>
6 </receiver>
如果发送的广播的action已经注册了,那么在onReceive()方法中就会接收到发过来的广播,继而做出一些处理。
你可以写出一个内部类:
1 class MyReceiver extends BroadcastReceiver {
2
3 @Override
4 publicvoid onReceive(Context context, Intent intent) {
5 // TODO Auto-generated method stub
6 //从这里的log中可以看到是否接受到了广播
7 System.out.println("intent = "+ intent);
8 }
9
10 }
2
3 @Override
4 publicvoid onReceive(Context context, Intent intent) {
5 // TODO Auto-generated method stub
6 //从这里的log中可以看到是否接受到了广播
7 System.out.println("intent = "+ intent);
8 }
9
10 }
当然也可以单独写一个类:
package com.android.TestBrocastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
publicclass MyBrocastReceiverTest extends BroadcastReceiver {
@Override
publicvoid onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("intent = "+ intent);
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
publicclass MyBrocastReceiverTest extends BroadcastReceiver {
@Override
publicvoid onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("intent = "+ intent);
}
}
一般如果在代码里面注册,就把广播写在代码里面,在xml文件中注册就单独写一个类。
注册在代码里面:
1 package com.android.TestBrocastReceiver;
2
3 import android.app.Activity;
4 import android.content.BroadcastReceiver;
5 import android.content.Context;
6 import android.content.Intent;
7 import android.content.IntentFilter;
8 import android.os.Bundle;
9
10 publicclass TestBrocastReceiver extends Activity {
11
12 MyReceiver myReceiver;
13
14 @Override
15 publicvoid onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.main);
18 }
19
20 @Override
21 protectedvoid onPause() {
22 // TODO Auto-generated method stub
23 unregisterReceiver(myReceiver);
24 super.onPause();
25 }
26
27 @Override
28 protectedvoid onResume() {
29 // TODO Auto-generated method stub
30
31 myReceiver =new MyReceiver();
32 IntentFilter filter =new IntentFilter();
33 //向过滤器中添加action
34 filter.addAction("com.android.shang");
35 //注册广播
36 registerReceiver(myReceiver, filter);
37
38 Intent intent =new Intent();
39 intent.setAction("com.android.xiang");
40 //发送广播
41 sendBroadcast(intent);
42 super.onResume();
43 }
44
45 class MyReceiver extends BroadcastReceiver {
46
47 @Override
48 publicvoid onReceive(Context context, Intent intent) {
49 // TODO Auto-generated method stub
50 //从这里的log中可以看到是否接受到了广播
51 System.out.println("intent = "+ intent);
52 }
53 }
54 }
2
3 import android.app.Activity;
4 import android.content.BroadcastReceiver;
5 import android.content.Context;
6 import android.content.Intent;
7 import android.content.IntentFilter;
8 import android.os.Bundle;
9
10 publicclass TestBrocastReceiver extends Activity {
11
12 MyReceiver myReceiver;
13
14 @Override
15 publicvoid onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.main);
18 }
19
20 @Override
21 protectedvoid onPause() {
22 // TODO Auto-generated method stub
23 unregisterReceiver(myReceiver);
24 super.onPause();
25 }
26
27 @Override
28 protectedvoid onResume() {
29 // TODO Auto-generated method stub
30
31 myReceiver =new MyReceiver();
32 IntentFilter filter =new IntentFilter();
33 //向过滤器中添加action
34 filter.addAction("com.android.shang");
35 //注册广播
36 registerReceiver(myReceiver, filter);
37
38 Intent intent =new Intent();
39 intent.setAction("com.android.xiang");
40 //发送广播
41 sendBroadcast(intent);
42 super.onResume();
43 }
44
45 class MyReceiver extends BroadcastReceiver {
46
47 @Override
48 publicvoid onReceive(Context context, Intent intent) {
49 // TODO Auto-generated method stub
50 //从这里的log中可以看到是否接受到了广播
51 System.out.println("intent = "+ intent);
52 }
53 }
54 }
注册在xml文件中:
在xml文件中注册,写上一个广播类,然后在activity中发送广播就可以了。
有时系统会发出一些广播,比如说时间的变化等,时间走一秒就会发一个广播,这时我们可以监听这个广播,然后在接收器中做处理就可以了,而不需要在发广播了。