Android之BroadcastReceiver的使用
一、BroadcastReceiver的简介
用于异步接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast()、广播接收者(BroadcastReceiver)用于异步接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast()、Context.sendOrderedBroadcast()或者Context.sendStickyBroadcast()来实现的。通常一个广播Intent可以被订阅了此Intent的多个广播接收者所接收,广播接收者和JMS中的Topic消息接收者很相似.
广播接收器只能接收广播,对广播的通知做出反应,很多广播都产生于系统代码.如:时区改变的通知,电池电量不足、用户改变了语言偏好或者开机启动等.
广播接收器没有用户界面,但是,它可以为它们接收到信息启动一个Activity或者使用NotificationManager来通知用户.
二、BroadcastReceiver的两种注册方式
1)静态注册
静态注册方式是在AndroidManifest.xml的application里面定义receiver并设置要接收的action
首先,新建一个Android项目--->取名BroadcastReceiverDemo01
BroadcastActivity.java的代码
- public class BroadcastActivity extends Activity {
- private Button send = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- send = (Button)findViewById(R.id.sned);
- send.setOnClickListener(new BroadcastListener());
- }
- class BroadcastListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- System.out.println("------------");
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_EDIT);
- BroadcastActivity.this.sendBroadcast(intent);
- }
- }
- }
TestReceiver.java的代码
- public class TestReceiver extends BroadcastReceiver {
- public TestReceiver() {
- System.out.println("TestReceiver create......");
- }
- @Override
- public void onReceive(Context arg0, Intent arg1) {
- System.out.println("receive......");
- }
- }
main.xml的布局文件
- <?xml version="1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android" - android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- android:id="@+id/sned"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/send"
- />
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
"hello" >Hello World, TestReceiver!"app_name" >broadcast"send" >发送
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android" - package="com.gem.activity"
- android:versionCode="1"
- android:versionName="1.0">
- "@drawable/icon" android:label="@string/app_name">
- ".BroadcastActivity"
- android:label="@string/app_name">
- "android.intent.action.MAIN" />
"android.intent.category.LAUNCHER" />".TestReceiver" >- "android.intent.action.EDIT">
"8" />
效果图
当用户点击发送的时候,程序会调用onReceive()方法 |
2)动态注册
动态注册方式在activity里面调用函数来注册,和静态的内容差不多。一个形参是receiver,另一个是IntentFilter,其中里面是要接收的action.
首先,新建一个Android项目--->取名BroadcastReceiverDemo02
BroadcastActivity.java的代码
- public class BroadcastActivity extends Activity {
- private Button send;
- private Button registerReceiver;
- private Button unregisterReceiver;
- private MyReceiver myReceiver;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- send = (Button)findViewById(R.id.send);
- send.setOnClickListener(new SendButtontListener());
- registerReceiver = (Button)findViewById(R.id.registerReceiver);
- registerReceiver.setOnClickListener(new RegisterReceiverButtonListener());
- unregisterReceiver = (Button)findViewById(R.id.unregisterReceiver);
- unregisterReceiver.setOnClickListener(new UnregisterReceiverButtonListener());
- }
- class SendButtontListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setAction("Intent.ACTION_EDIT");
- BroadcastActivity.this.sendBroadcast(intent);
- System.out.println("send-----");
- }
- }
- class RegisterReceiverButtonListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- myReceiver = new MyReceiver();
- IntentFilter filter = new IntentFilter();
- filter.addAction("Intent.ACTION_EDIT");
- //动态注册BroadcastReceiver
- registerReceiver(myReceiver, filter);
- }
- }
- class UnregisterReceiverButtonListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- //注销BroadcastReceiver
- unregisterReceiver(myReceiver);
- System.out.println("close-----");
- }
- }
- }
MyReceiver.java的代码
- public class MyReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- System.out.println("onReceive......");
- }
- }
main.xml的布局文件
- <?xml version="1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android" - android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- android:id="@+id/send"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/send"
- />
- android:id="@+id/registerReceiver"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/registerReceiver"
- />
- android:id="@+id/unregisterReceiver"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/unregisterReceiver"
- />
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
"hello" >Hello World, TestReceiver!"app_name" >broadcast"send" >发送广播"registerReceiver" >注册广播接收器"unregisterReceiver" >注销广播接收器
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android" - package="com.gem.activity"
- android:versionCode="1"
- android:versionName="1.0">
- "@drawable/icon" android:label="@string/app_name">
- ".BroadcastActivity"
- android:label="@string/app_name">
- "android.intent.action.MAIN" />
"android.intent.category.LAUNCHER" />"8" />
效果图
当用户点击发送的时候,程序没有注册BroadcastReceiver,当用户点击注册广播接收器之后在点击发送会调用MyReceiver中的onReceive()方法,当用户点击注销广播接收器之后程序执行unregisterReceiver()方法 |
更多相关文章
- Android之记住密码与自动登陆实现
- Android(安卓)permission 权限类及中英文说明
- Android实现拨打电话的两种方式
- Android(安卓)SELinux
- Android(安卓)BroadcastReceiver小结
- Activity学习(一):生命周期
- Android(安卓)逐帧动画isRunning 一直返回true的问题
- android用户界面之GridView教程实例汇总
- Android:Xmpp协议的简单分析