手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android开发应用中Broadcast Receiver组件详解
Android开发应用中Broadcast Receiver组件详解
摘要:BroadcastReceiver(广播接收器)是Android中的四大组件之一。下面就具体介绍一下BroadcastReceiver组件的...

BroadcastReceiver(广播接收器)是Android中的四大组件之一。下面就具体介绍一下Broadcast Receiver组件的用法。

下面是Android Doc中关于BroadcastReceiver的概述:

①广播接收器是一个专注于接收广播通知信息,并做出对应处理的组件。很多广播是源自于系统代码的──比如,通知时区改变、电池电量低、拍摄了一张照片或者用户改变了语言选项。应用程序也可以进行广播──比如说,通知其它应用程序一些数据下载完成并处于可用状态。

②应用程序可以拥有任意数量的广播接收器以对所有它感兴趣的通知信息予以响应。所有的接收器均继承自BroadcastReceiver基类。

③广播接收器没有用户界面。然而,它们可以启动一个activity来响应它们收到的信息,或者用NotificationManager来通知用户。通知可以用很多种方式来吸引用户的注意力──闪动背灯、震动、播放声音等等。一般来说是在状态栏上放一个持久的图标,用户可以打开它并获取消息。

Android中的广播事件有两种,一种就是系统广播事件,比如:ACTION_BOOT_COMPLETED(系统启动完成后触发),ACTION_TIME_CHANGED(系统时间改变时触发),ACTION_BATTERY_LOW(电量低时触发)等等。另外一种是我们自定义的广播事件。

广播事件的流程

①注册广播事件:注册方式有两种,一种是静态注册,就是在AndroidManifest.xml文件中定义,注册的广播接收器必须要继承BroadcastReceiver;另一种是动态注册,是在程序中使用Context.registerReceiver注册,注册的广播接收器相当于一个匿名类。两种方式都需要IntentFIlter。

②发送广播事件:通过Context.sendBroadcast来发送,由Intent来传递注册时用到的Action。

③接收广播事件:当发送的广播被接收器监听到后,会调用它的onReceive()方法,并将包含消息的Intent对象传给它。onReceive中代码的执行时间不要超过5s,否则Android会弹出超时dialog。

下面我通过代码演示自定义广播事件和系统广播事件的使用。

Step1:在MainActivity的onStart方法中注册广播事件。静态注册方式是在AndroidManifest.xml文件中。

Step2: 点击相应按钮后会触发相应的方式来发送广播消息。

/** * MainActivity * @author zuolongsnail * */ public class MainActivity extends Activity { private Button sendStaticBtn; private Button sendDynamicBtn; private Button sendSystemBtn; private static final String STATICACTION = "com.byread.static"; private static final String DYNAMICACTION = "com.byread.dynamic"; // USB设备连接 private static final String SYSTEMACTION = Intent.ACTION_POWER_CONNECTED; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sendStaticBtn = (Button) findViewById(R.id.send_static); sendDynamicBtn = (Button) findViewById(R.id.send_dynamic); sendSystemBtn = (Button) findViewById(R.id.send_system); sendStaticBtn.setOnClickListener(new MyOnClickListener()); sendDynamicBtn.setOnClickListener(new MyOnClickListener()); sendSystemBtn.setOnClickListener(new MyOnClickListener()); } class MyOnClickListener implements OnClickListener{ @Override public void onClick(View v) { // 发送自定义静态注册广播消息 if(v.getId() == R.id.send_static){ Log.e("MainActivity", "发送自定义静态注册广播消息"); Intent intent = new Intent(); intent.setAction(STATICACTION); intent.putExtra("msg", "接收静态注册广播成功!"); sendBroadcast(intent); } // 发送自定义动态注册广播消息 else if(v.getId() == R.id.send_dynamic){ Log.e("MainActivity", "发送自定义动态注册广播消息"); Intent intent = new Intent(); intent.setAction(DYNAMICACTION); intent.putExtra("msg", "接收动态注册广播成功!"); sendBroadcast(intent); } // 发送系统动态注册广播消息。当手机连接充电设备时会由系统自己发送广播消息。 else if(v.getId() == R.id.send_system){ Log.e("MainActivity", "发送系统动态注册广播消息"); Intent intent = new Intent(); intent.setAction(SYSTEMACTION); intent.putExtra("msg", "正在充电。。。。"); } } } @Override protected void onStart() { super.onStart(); Log.e("MainActivity", "注册广播事件"); // 注册自定义动态广播消息 IntentFilter filter_dynamic = new IntentFilter(); filter_dynamic.addAction(DYNAMICACTION); registerReceiver(dynamicReceiver, filter_dynamic); // 注册系统动态广播消息 IntentFilter filter_system = new IntentFilter(); filter_system.addAction(SYSTEMACTION); registerReceiver(systemReceiver, filter_system); } private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.e("MainActivity", "接收自定义动态注册广播消息"); if(intent.getAction().equals(DYNAMICACTION)){ String msg = intent.getStringExtra("msg"); Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } } }; private BroadcastReceiver systemReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.e("MainActivity", "接收系统动态注册广播消息"); if(intent.getAction().equals(SYSTEMACTION)){ String msg = intent.getStringExtra("msg"); Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } } }; }

Step3:接收广播消息。以下为两个静态注册的广播接收器。

/** * 自定义静态注册广播消息接收器 * @author zuolongsnail * */ public class StaticReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String msg = intent.getStringExtra("msg"); Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } }

/** * 系统静态注册广播消息接收器 * * @author zuolongsnail * */ public class SystemReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_BATTERY_LOW)) { Log.e("SystemReceiver", "电量低提示"); Toast.makeText(context, "您的手机电量偏低,请及时充电", Toast.LENGTH_SHORT).show(); } } }

下面是AndroidManifest.xml文件:

<"1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.byread" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <> <receiver android:name=".StaticReceiver"> <intent-filter> <action android:name="com.byread.static" /> </intent-filter> </receiver> <> <receiver android:name=".SystemReceiver"> <intent-filter> <action android:name="android.intent.action.BATTERY_LOW" /> </intent-filter> </receiver> </application> </manifest>

界面布局文件main.xml

<"1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/send_static" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送自定义静态注册广播" /> <Button android:id="@+id/send_dynamic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送自定义动态注册广播" /> <Button android:id="@+id/send_system" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送系统动态注册广播" /> </LinearLayout>

讲解结束,不过有一点我自己也没弄清楚,这个系统广播事件如果我在程序中sendBroadcast的话,那就是自定义广播了。如果不写的话,那是不是系统自己来发送对应Action广播呢?有知道的同学请告诉我一下,再此先谢过。

运行界面:

Android开发应用中Broadcast Receiver组件详解1

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持查字典教程网。

【Android开发应用中Broadcast Receiver组件详解】相关文章:

Android开发 旋转屏幕导致Activity重建解决方法

Android开发技巧之ViewStub控件惰性装载

Android 动画之TranslateAnimation应用详解

解析Android开发优化之:对界面UI的优化详解(二)

Android开发之选项组件

android图库竖屏不显示status bar的解决方法

Android开发笔记之:一分钟学会使用Logcat调试程序的详解

Android开发:Android SDK的介绍

Android应用程序的调试

Android 原始资源文件的使用详解

精品推荐
分类导航