Android BroadcastReceiver 的生命周期及实际应用
来源:互联网 发布:linux json 格式化 编辑:程序博客网 时间:2024/05/12 13:35
BroadcastReceiver 有一个回调方法:void onReceive(Context curContext, Intent broadcastMsg)。当一个广播消息到达接收者时,Android 调用它的 onReceive() 方法并传递给它包含消息的 Intent 对象。BroadcastReceiver 被认为仅当它执行这个方法时是活跃的。当 onReceive() 返回后,它是不活跃的。
活跃的 BroadcastReceiver 的进程是受保护的,不会被杀死。但是系统可以在任何时候杀死有不活跃组件的进程。
这带来一个问题,当一个广播消息的响应时费时的,因此应该在独立的线程中做这些事,远离用户界面或者其它组件运行的主线程。如果 onReceive() 衍生线程然后返回,整个进程,包括新的线程,被判定为不活跃的(除非进程中的其它应用程序组件是活跃的),否则将使它处于被杀的危机。解决这个问题的方法是 onReceive() 启动一个 Service,让 Service 做这个工作,因此系统知道进程中有活跃的工作在做。
BroadcastReceiver 和事件处理机制类似,只不过事件的处理机制是应用程序级别的,而广播处理机制是系统级别的。
BroadcastReceiver 用于接收并处理广播通知(broadcast announcements)。多数的广播是系统发起的,如地域变换、电量不足、来电来信等,当然,应用程序自己也可以广播一个广播。BroadcastReceiver 可以通过多种方式通知用户:启动activity、使用NotificationManager、开启背景灯、振动设备、播放声音等,最典型的是在状态栏显示一个图标,这样用户就可以点它打开看通知内容。
通常某个应用或系统本身在某些事件(电池电量不足、来电来短信)来临时会广播一个 Intent 出去,通过注册一个 BroadcastReceiver 来监听到这些 Intent,并获取其中广播的数据。
Android BroadcastReceiver 实际应用
不同进程之间通过 BroadcastReceiver 传递信息,可以通过以下两种方式来实现:
第一种方式:在 AndroidManifest.xml 中注册 BroadcastReceiver,这里推荐使用这种方法,因为它不需要手动注销广播(如果广播未注销,程序退出时可能会出错)。
- <receiver android:name=".mEvtReceiver">
- <intent -filter>
- <action android:name="android.intent.action.BOOT_COMPLETED" />
- </intent>
- </receiver>
上面两个 android:name 分别是广播名和广播的动作(这里的动作是表示系统启动完成),如果要自己发送一个广播,在代码中为:
- Intent i = new Intent("android.intent.action.BOOT_COMPLETED");
- sendBroadcast(i);
这样,广播就发出去了,然后是接收。接收可以新建一个类,继承 BroadcastReceiver,也可以实例化一个 BroadcastReceiver 对象,代码如下:
- protected BroadcastReceiver mEvtReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (action.equals("android.intent.action.BOOT_COMPLETED")) {
- //Do something
- }
- }
- };
完整实例:
- < ?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="android.basic.lesson21"
- android:versionCode="1"
- android:versionName="1.0">
- <uses -sdk android:minSdkVersion="8" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainBroadcastReceiver"
- android:label="@string/app_name">
- <intent -filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent>
- </activity>
- <receiver android:name=".HelloBroadReciever">
- <intent -filter>
- <action android:name="android.basic.lesson21.Hello88" />
- </intent>
- </receiver>
- </application>
- </manifest>
- package android.basic.lesson21;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class MainBroadcastReceiver extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button b1 = (Button) findViewById(R.id.Button01);
- b1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 定义一个intent
- Intent intent = new Intent().setAction("android.basic.lesson21.Hello88")
- .putExtra("yaoyao","yaoyao is 189 days old ,27 weeks -- 2010-08-10");
- System.out.println("sendBroadcast");
- // 广播出去
- sendBroadcast(intent);
- }
- });
- }
- }
- package android.basic.lesson21;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- public class HelloBroadReciever extends BroadcastReceiver {
- public HelloBroadReciever() {
- System.out.println("HelloBroadReciever");
- }
- // 如果接收的事件发生
- @Override
- public void onReceive(Context context, Intent intent) {
- // 对比Action决定输出什么信息
- if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
- Log.i("HelloBroadReciever","BOOT_COMPLETED !!!!!!!!!!!!!!!!!!!!!!!!!");
- }
- if (intent.getAction().equals("android.basic.lesson21.Hello88")) {
- Log.i("HelloBroadReciever","Say Hello to Yaoyao !!!!!!!!!!!!!!!!!!!!!!!!!");
- Log.i("HelloBroadReciever", intent.getStringExtra("yaoyao"));
- }
- Log.i("HelloBroadReciever","onReceive**********");
- // 播放一首音乐
- // MediaPlayer.create(context, R.raw.babayetu).start();
- }
- }
第二种方式,直接在代码中实现,但需要手动注册注销,以短信接收为例,实现如下:
- IntentFilter filter = new IntentFilter();
- filter.addAction("android.provider.Telephony.SMS_RECEIVED");
- registerReceiver(mEvtReceiver, filter); //这时注册了一个recevier ,名为mEvtReceiver,然后同样用上面的方法以重写onReceiver,
- 最后在程序的onDestroy中要注销广播,实现如下:
- @Override
- public void onDestroy() {
- super.onDestroy();
- unregisterReceiver(mPlayerEvtReceiver);
- }
完整实例:
- < ?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="android.basic.lesson21"
- android:versionCode="1"
- android:versionName="1.0">
- <uses -sdk android:minSdkVersion="5" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainBroadcastReceiver"
- android:label="@string/app_name">
- <intent -filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent>
- </activity>
- </application>
- <uses -permission android:name="android.permission.RECEIVE_SMS"/>
- </manifest>
- package android.basic.lesson21;
- import android.app.Activity;
- import android.content.IntentFilter;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- public class MainBroadcastReceiver extends Activity {
- /** Called when the activity is first created. */
- HelloBroadReciever br;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button b1 = (Button) findViewById(R.id.Button01);
- b1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Log.i("click","OnClick");
- br=new HelloBroadReciever();
- IntentFilter filter=new IntentFilter();
- filter.addAction("android.provider.Telephony.SMS_RECEIVED");
- registerReceiver(br, filter);
- }
- });
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- unregisterReceiver(br);
- Log.i("br","onDestroy");
- }
- }
- package android.basic.lesson21;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- public class HelloBroadReciever extends BroadcastReceiver {
- public HelloBroadReciever(){
- System.out.println("HelloBroadReciever");
- }
- // 如果接收的事件发生
- @Override
- public void onReceive(Context context, Intent intent) {
- // 对比Action决定输出什么信息
- if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
- Log.i("HelloBroadReciever","SMS_RECEIVED !!!!!!!!!!!!!!!!!!!!!!!!!");
- }
- Log.i("HelloBroadReciever","onReceive**********");
- }
- }
- Android BroadcastReceiver 的生命周期及实际应用
- Android BroadcastReceiver 的生命周期及实际应用
- android broadcastReceiver生命周期及两种应用
- 【Android】BroadcastReceiver的生命周期
- android BroadcastReceiver 生命周期的问题
- android中Activity的生命周期及应用
- Android BroadcastReceiver 的简单应用
- Android BroadcastReceiver生命周期分析
- 的用途及实际应用
- android 监听网络状态的变化及实际应用
- android 监听网络状态的变化及实际应用
- BroadcastReceiver的生命周期
- BroadcastReceiver的生命周期
- BroadcastReceiver 的生命周期
- BroadcastReceiver生命周期的探究
- android 应用的生命周期
- Fragment实际应用中的生命周期
- Android 应用进程生命周期及static的生命周期引发的问题及解决思路
- 使用outline稳固sql执行计划
- Lustre可靠性增强系统MTFS:第5篇 在Lustre文件系统中的使用
- jsp的含有验证、md5的登录例子
- ASP.NET如何防止SQL注入
- #error
- Android BroadcastReceiver 的生命周期及实际应用
- Fedora 16下安装Apache+MySql+PHP环境及配置
- Ubuntu系统建立FTP服务器学习教程
- 设计模式要素及常见的设计模式
- 【C/C++和指针】printf 的那些事
- erlang的简单数据类型
- svn安装
- android wifi之WifiMonitor
- jsp中的一些页面的语法的讲解和连接的讲解