在 Android 中使用 Activity, Service, Broadcast, BroadcastReceiver
活动(Activity) - 用于表现功能
服务(Service) - 相当于后台运行的 Activity
广播(Broadcast) - 用于发送广播
广播接收器(BroadcastReceiver) - 用于接收广播
Intent - 用于连接以上各个组件,并在其间传递消息
==========================================================================
BroadcastReceiver:
在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制。而BroadcastReceiver是对发送出来的 Broadcast进行过滤接受并响应的一类组件。下面将详细的阐述如何发送Broadcast和使用BroadcastReceiver过滤接收的过程:
首先在需要发送信息的地方,把要发送的信息和用于过滤的信息(如Action、Category)装入一个Intent对象,然后通过调用 Context.sendBroadcast()、sendOrderBroadcast()或sendStickyBroadcast()方法,把 Intent对象以广播方式发送出去。
当Intent发送以后,所有已经注册的BroadcastReceiver会检查注册时的IntentFilter是否与发送的Intent相匹配,若匹配则就会调用BroadcastReceiver的onReceive()方法。所以当我们定义一个BroadcastReceiver的时候,都需要实现onReceive()方法。
注册BroadcastReceiver有两种方式:
一种方式是,静态的在AndroidManifest.xml中用<receiver>标签生命注册,并在标签内用<intent- filter>标签设置过滤器。
另一种方式是,动态的在代码中先定义并设置好一个 IntentFilter 对象,然后在需要注册的地方调Context.registerReceiver()方法,如果取消时就调用 Context.unregisterReceiver()方法。如果用动态方式注册的BroadcastReceiver的Context对象被销毁时,BroadcastReceiver也就自动取消注册了。(特别注意,有些可能需要后台监听的,如短信消息)
另外,若在使用sendBroadcast()的方法是指定了接收权限,则只有在AndroidManifest.xml中用<uses- permission>标签声明了拥有此权限的BroascastReceiver才会有可能接收到发送来的Broadcast。同样,若在注册BroadcastReceiver时指定了可接收的Broadcast的权限,则只有在包内的AndroidManifest.xml中 用<uses-permission>标签声明了,拥有此权限的Context对象所发送的Broadcast才能被这个 BroadcastReceiver所接收。
1.静态注册BroadcastReceiver:
静态注册比动态注册麻烦点,先新建一个类继承BroadcastReceiver,如:
clsReceiver2.java
package com.testBroadcastReceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; /* * 接收静态注册广播的BroadcastReceiver, * step1:要到AndroidManifest.xml这里注册消息 * <receiver android:name="clsReceiver2"> <intent-filter> <action android:name="com.testBroadcastReceiver.Internal_2"/> </intent-filter> </receiver> step2:定义消息的字符串 step3:通过Intent传递消息来驱使BroadcastReceiver触发 */ public class clsReceiver2 extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Toast.makeText(context, " 静态: " + action, 1000 ).show(); } }
然后到AndroidManifest.xml 添加receive标签
< receiver android:name = " clsReceiver2 " > < intent - filter > < action android:name = " com.testBroadcastReceiver.Internal_2 " /> </ intent - filter > </ receiver >
第一个name是类名,即你的继承BroadcastReceiver的类的名字,里面实现了BroadcastReceive的onReceive()方法,来处理你接到消息的动作。
第二个name是action的名称,即你要监听的消息名字(其它消息都会被过滤不监听)。
2.动态注册BroadcastReceiver:
主要代码部分为:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(String); //为BroadcastReceiver指定action,即要监听的消息名字。
registerReceiver(MyBroadcastReceiver,intentFilter); //注册监听
unregisterReceiver(MyBroadcastReceiver); //取消监听
(一般:在onStart中注册,onStop中取消unregisterReceiver)
private class MyBroadcastReceive extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(intent.ACTION_BATTERY_CHANGED.equals(action)) //判断是否接到电池变换消息
{
//处理内容
}
}
}
============================================================================================
Android中定义好了一些标准广播常量,可以让我们方便的使用,值和意义总结如下:
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <action android:name="android.intent.action.PHONE_STATE" /> //电话状态变化,有来电或者拨出去
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> //拨打电话
<uses-permission android:name="android.permission.RECEIVE_SMS" /> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> //收到信息
Intent.ACTION_AIRPLANE_MODE_CHANGED; //关闭或打开飞行模式时的广播 Intent.ACTION_BATTERY_CHANGED; //充电状态,或者电池的电量发生变化 //电池的充电状态、电荷级别改变,不能通过组建声明接收这个广播,只有通过Context.registerReceiver()注册 Intent.ACTION_BATTERY_LOW; //表示电池电量低 Intent.ACTION_BATTERY_OKAY; //表示电池电量充足,即从电池电量低变化到饱满时会发出广播 Intent.ACTION_BOOT_COMPLETED; //在系统启动完成后,这个动作被广播一次(只有一次)。 Intent.ACTION_CAMERA_BUTTON; //按下照相时的拍照按键(硬件按键)时发出的广播 Intent.ACTION_CLOSE_SYSTEM_DIALOGS; //当屏幕超时进行锁屏时,当用户按下电源按钮,长按或短按(不管有没跳出话框),进行锁屏时,android系统都会广播此Action消息 Intent.ACTION_CONFIGURATION_CHANGED; //设备当前设置被改变时发出的广播(包括的改变:界面语言,设备方向,等,请参考Configuration.java) Intent.ACTION_DATE_CHANGED; //设备日期发生改变时会发出此广播 Intent.ACTION_DEVICE_STORAGE_LOW; //设备内存不足时发出的广播,此广播只能由系统使用,其它APP不可用? Intent.ACTION_DEVICE_STORAGE_OK; //设备内存从不足到充足时发出的广播,此广播只能由系统使用,其它APP不可用? Intent.ACTION_DOCK_EVENT; // //发出此广播的地方frameworks\base\services\java\com\android\server\DockObserver.java Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE; ////移动APP完成之后,发出的广播(移动是指:APP2SD) Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE; //正在移动APP时,发出的广播(移动是指:APP2SD) Intent.ACTION_GTALK_SERVICE_CONNECTED; //Gtalk已建立连接时发出的广播 Intent.ACTION_GTALK_SERVICE_DISCONNECTED; //Gtalk已断开连接时发出的广播 Intent.ACTION_HEADSET_PLUG; //在耳机口上插入耳机时发出的广播 Intent.ACTION_INPUT_METHOD_CHANGED; //改变输入法时发出的广播 Intent.ACTION_LOCALE_CHANGED; //设备当前区域设置已更改时发出的广播 Intent.ACTION_MANAGE_PACKAGE_STORAGE; // Intent.ACTION_MEDIA_BAD_REMOVAL; //未正确移除SD卡(正确移除SD卡的方法:设置--SD卡和设备内存--卸载SD卡),但已把SD卡取出来时发出的广播 //广播:扩展介质(扩展卡)已经从 SD 卡插槽拔出,但是挂载点 (mount point) 还没解除 (unmount) Intent.ACTION_MEDIA_BUTTON; //按下"Media Button" 按键时发出的广播,假如有"Media Button"按键的话(硬件按键) Intent.ACTION_MEDIA_CHECKING; //插入外部储存装置,比如SD卡时,系统会检验SD卡,此时发出的广播? Intent.ACTION_MEDIA_EJECT; //已拔掉外部大容量储存设备发出的广播(比如SD卡,或移动硬盘),不管有没有正确卸载都会发出此广播? //广播:用户想要移除扩展介质(拔掉扩展卡)。 Intent.ACTION_MEDIA_MOUNTED; //插入SD卡并且已正确安装(识别)时发出的广播 //广播:扩展介质被插入,而且已经被挂载。 Intent.ACTION_MEDIA_NOFS; // Intent.ACTION_MEDIA_REMOVED; //外部储存设备已被移除,不管有没正确卸载,都会发出此广播? // 广播:扩展介质被移除。 Intent.ACTION_MEDIA_SCANNER_FINISHED; //广播:已经扫描完介质的一个目录 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE; // Intent.ACTION_MEDIA_SCANNER_STARTED; //广播:开始扫描介质的一个目录 Intent.ACTION_MEDIA_SHARED; // 广播:扩展介质的挂载被解除 (unmount),因为它已经作为 USB 大容量存储被共享。 Intent.ACTION_MEDIA_UNMOUNTABLE; // Intent.ACTION_MEDIA_UNMOUNTED // 广播:扩展介质存在,但是还没有被挂载 (mount)。 Intent.ACTION_NEW_OUTGOING_CALL; // Intent.ACTION_PACKAGE_ADDED; //成功的安装APK之后 //广播:设备上新安装了一个应用程序包。 //一个新应用包已经安装在设备上,数据包括包名(最新安装的包程序不能接收到这个广播) Intent.ACTION_PACKAGE_CHANGED; //一个已存在的应用程序包已经改变,包括包名 Intent.ACTION_PACKAGE_DATA_CLEARED; //清除一个应用程序的数据时发出的广播(在设置--应用管理--选中某个应用,之后点清除数据时?) //用户已经清除一个包的数据,包括包名(清除包程序不能接收到这个广播) Intent.ACTION_PACKAGE_INSTALL; //触发一个下载并且完成安装时发出的广播,比如在电子市场里下载应用? // Intent.ACTION_PACKAGE_REMOVED; //成功的删除某个APK之后发出的广播 //一个已存在的应用程序包已经从设备上移除,包括包名(正在被安装的包程序不能接收到这个广播) Intent.ACTION_PACKAGE_REPLACED; //替换一个现有的安装包时发出的广播(不管现在安装的APP比之前的新还是旧,都会发出此广播?) Intent.ACTION_PACKAGE_RESTARTED; //用户重新开始一个包,包的所有进程将被杀死,所有与其联系的运行时间状态应该被移除,包括包名(重新开始包程序不能接收到这个广播) Intent.ACTION_POWER_CONNECTED; //插上外部电源时发出的广播 Intent.ACTION_POWER_DISCONNECTED; //已断开外部电源连接时发出的广播 Intent.ACTION_PROVIDER_CHANGED; // Intent.ACTION_REBOOT; //重启设备时的广播 Intent.ACTION_SCREEN_OFF; //屏幕被关闭之后的广播 Intent.ACTION_SCREEN_ON; //屏幕被打开之后的广播 Intent.ACTION_SHUTDOWN; //关闭系统时发出的广播 Intent.ACTION_TIMEZONE_CHANGED; //时区发生改变时发出的广播 Intent.ACTION_TIME_CHANGED; //时间被设置时发出的广播 Intent.ACTION_TIME_TICK; //广播:当前时间已经变化(正常的时间流逝)。 //当前时间改变,每分钟都发送,不能通过组件声明来接收,只有通过Context.registerReceiver()方法来注册 Intent.ACTION_UID_REMOVED; //一个用户ID已经从系统中移除发出的广播 // Intent.ACTION_UMS_CONNECTED; //设备已进入USB大容量储存状态时发出的广播? Intent.ACTION_UMS_DISCONNECTED; //设备已从USB大容量储存状态转为正常状态时发出的广播? Intent.ACTION_USER_PRESENT; // Intent.ACTION_WALLPAPER_CHANGED; //设备墙纸已改变时发出的广播 PS: 经本人测试,ACTION_SCREEN_ON 和 ACTION_SCREEN_OFF 这两个广播也只是“不能通过组建声明接收这个广播,只有通过Context.registerReceiver()注册”
<!-- 关于程序包事件 --> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <action android:name="android.intent.action.PACKAGE_CHANGED" /> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <action android:name="android.intent.action.PACKAGE_DATA_CLEARED" /> <action android:name="android.intent.action.PACKAGE_INSTALL" /> <action android:name="android.intent.action.PACKAGE_REPLACED" /> <action android:name="android.intent.action.PACKAGE_RESTARTED" /> <data android:scheme="package"></data> </intent-filter> <!-- 隐秘数字事件 --> <intent-filter> <action android:name="android.provider.Telephony.SECRET_CODE"></action> <data android:scheme="android_secret_code" android:host="0101"></data> </intent-filter> <!-- 收发短信、SIM卡存储已满、WAP PUSH的接收事件,<br/> 需要android.permission.RECEIVER_SMS及android.permission.RECEIVER_MMS权限 --> <intent-filter> <!-- 无法模仿 --> <action android:name="android.provider.Telephony.SIM_FULL"></action> <!-- SMS_REJECTED:An incoming SMS has been rejected by the telephony framework. This intent is sent in lieu of any of the RECEIVED_ACTION intents.(无法模仿) --> <action android:name="android.provider.Telephony.SMS_REJECTED"></action> <action android:name="android.provider.Telephony.SMS_RECEIVED"></action> </intent-filter> <intent-filter> <!-- 向10086发送短信012345即可有WAP_PUSH,需要android.permission.RECEIVE_WAP_PUSH权限 --> <action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED"></action> <data android:mimeType="application/vnd.wap.sic"></data> </intent-filter> <intent-filter> <!-- 向10086发送短信012345即可有WAP_PUSH,需要android.permission.RECEIVE_WAP_PUSH权限,未证实 --> <action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED"></action> <data android:mimeType="application/vnd.wap.sic"></data> </intent-filter> <intent-filter> <!-- 彩信,需要android.permission.RECEIVE_WAP_PUSH权限,未证实 --> <action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED"></action> <data android:mimeType="application/vnd.wap.mms-message"></data> </intent-filter> <intent-filter> <!-- 开机自启动,需要android.permission.RECEIVE_BOOT_COMPLETED --> <action android:name="android.intent.action.BOOT_COMPLETED"></action> <action android:name="android.intent.action.PRE_BOOT_COMPLETED"></action> <action android:name="android.intent.action.REBOOT"></action> </intent-filter> <intent-filter> <!-- 针对摩托罗拉等有相机快捷键的手机 --> <action android:name="android.intent.action.CAMERA_BUTTON"></action> <action android:name="android.intent.action.CALL_BUTTON"></action> </intent-filter> <intent-filter> <!-- 设置时间、时区时触发 --> <action android:name="android.intent.action.TIME_SET"></action> <action android:name="android.intent.action.TIMEZONE_CHANGED"></action> <action android:name="android.intent.action.LOCALE_CHANGED"></action> <action android:name="android.intent.action.DATE_CHANGED"></action> </intent-filter> <intent-filter> <!-- 来去电事件监听,需要android.permission.READ_PHONE_STATE权限 --> <action android:name="android.intent.action.PHONE_STATE"></action> <!-- 开关Wifi时触发 --> <action android:name="android.net.wifi.WIFI_STATE_CHANGED"></action> <!-- 电话的信号强度已经改变。 --> <action android:name="android.intent.action.SIG_STR"></action> <!-- 电话服务的状态已经改变。(调为飞行模式可观察到变化) --> <action android:name="android.intent.action.SERVICE_STATE"></action> <!-- 当改变AIRPLANE_MODE时触发 --> <action android:name="android.intent.action.AIRPLANE_MODE"></action> </intent-filter> <intent-filter> <action android:name="android.intent.action.WALLPAPER_CHANGED"></action> </intent-filter> <intent-filter> <action android:name="android.intent.action.BATTERY_CHANGED"></action> <action android:name="android.intent.action.BATTERY_LOW"></action> <action android:name="android.intent.action.BATTERY_OKAY"></action> </intent-filter> <intent-filter> <!-- 与充电器或USB连接时触发 --> <action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action> <action android:name="android.intent.action.POWER_USAGE_SUMMARY"></action> <action android:name="android.intent.action.ACTION_SHUTDOWN"></action> </intent-filter> <intent-filter> <!-- (USB mass storage)未模拟 --> <action android:name="android.intent.action.UMS_CONNECTED"></action> <action android:name="android.intent.action.UMS_DISCONNECTED"></action> </intent-filter> <intent-filter> <!-- 未模拟 --> <action android:name="android.intent.action.DEVICE_STORAGE_LOW"></action> <action android:name="android.intent.action.DEVICE_STORAGE_OK"></action> </intent-filter> <intent-filter> <!-- 呼叫转接状态已被更改,未模拟 --> <action android:name="android.intent.action.CFF"></action> <!-- 不明其意,未模拟 --> <action android:name="android.intent.action.DATA_STATE"></action> <!-- 不明其意,未模拟 --> <action android:name="android.intent.action.DATA_ACTIVITY"></action> </intent-filter> <intent-filter> <!-- 不明其意,未模拟 --> <action android:name="android.intent.action.CONFIGURATION_CHANGED"></action> </intent-filter> <intent-filter> <!-- 以下内容有反应,规律不明,适用于电子市场 --> <!-- 取消所有被挂起的 (pending) 更新下载。 --> <action android:name="android.server.checkin.FOTA_CANCEL"></action> <!-- 更新已经被确认,马上就要开始安装。 --> <action android:name="android.server.checkin.FOTA_INSTALL"></action> <!-- 更新已经被下载,可以开始安装。 --> <action android:name="android.server.checkin.FOTA_READY"></action> <!-- 恢复已经停止的更新下载。 --> <action android:name="android.server.checkin.FOTA_RESTART"></action> <!-- 通过 OTA 下载并安装操作系统更新。 --> <action android:name="android.server.checkin.FOTA_UPDATE"></action> </intent-filter> <intent-filter> <action android:name="android.intent.action.SYNC_STATE_CHANGED"></action> <action android:name="com.android.sync.SYNC_CONN_STATUS_CHANGED"></action> </intent-filter> <intent-filter> <action android:name="android.intent.action.HEADSET_PLUG"></action> </intent-filter> <intent-filter> <!-- 选择静音时触发 --> <action android:name="android.media.RINGER_MODE_CHANGED"></action> </intent-filter> <intent-filter> <!-- 扩展介质(扩展卡)已经从 SD 卡插槽拔出,但是挂载点 (mount point) 还没解除 (unmount)。 --> <action android:name="android.intent.action.MEDIA_BAD_REMOVAL"></action> <!-- 用户想要移除扩展介质(拔掉扩展卡)。 --> <action android:name="android.intent.action.MEDIA_EJECT"></action> <!-- 扩展介质被插入,而且已经被挂载。 --> <action android:name="android.intent.action.MEDIA_MOUNTED"></action> <!-- 扩展介质被移除。 --> <action android:name="android.intent.action.MEDIA_REMOVED"></action> <!-- 已经扫描完介质的一个目录。 --> <action android:name="android.intent.action.MEDIA_SCANNER_FINISHED"></action> <!-- 开始扫描介质的一个目录 --> <action android:name="android.intent.action.MEDIA_SCANNER_STARTED"></action> <!-- 扩展介质的挂载被解除 (unmount),因为它已经作为 USB 大容量存储被共享。 --> <action android:name="android.intent.action.MEDIA_SCANNER_SHARED"></action> <!-- 扩展介质存在,但是还没有被挂载 (mount)。 --> <action android:name="android.intent.action.MEDIA_UNMOUNTED"></action> </intent-filter> <intent-filter> <!-- 不了解,未证实 --> <action android:name="android.intent.action.MEDIA_BUTTON"></action> <action android:name="android.media.AUDIO_BECOMING_NOISY" /> </intent-filter> <intent-filter> <!-- 设备收到了新的网络 "tickle" 通知,未模拟 --> <action android:name="android.intent.action.NETWORK_TICKLE_RECEIVED"></action> </intent-filter> <intent-filter> <!-- 条件不允许,未模拟 --> <!-- 更新将要(真正)被安装。 --> <action android:name="android.intent.action.PROVIDER_CHANGED"></action> <!-- 要求 polling of provisioning service 下载最新的设置。 --> <action android:name="android.intent.action.PROVISIONING_CHECK"></action> </intent-filter> <!-- 其它广播事件,不明其意,未模拟 --> <intent-filter> <!-- XMPP 连接已经被建立。 --> <action android:name="android.intent.action.XMPP_CONNECTED"></action> <!-- XMPP 连接已经被断开。 --> <action android:name="andandroid.intent.action.XMPP_DI"></action> <!-- 统计信息服务的状态已经改变。 --> <action android:name="android.intent.action.STATISTICS_STATE_CHANGED"></action> <!-- 要求 receivers 报告自己的统计信息。 --> <action android:name="android.intent.action.STATISTICS_REPORT"></action> <action android:name="android.intent.action.GTALK_DISCONNECTED"></action> <action android:name="android.intent.action.GTALK_CONNECTED"></action> <action android:name="android.intent.action.GET_CONTENT"></action> <action android:name="android.intent.action.MANAGE_PACKAGE_STORAGE"></action> <action android:name="android.intent.action.UID_REMOVED"></action> <action android:name="android.intent.action.USER_PRESENT"></action> </intent-filter>