第一种方法:
1.查找需要的Broadcast,见附录1
2.在AndroidManefest.xml里添加<receiver>...<receiver/>
<receiver android:name=".BootComplementReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
3.继承BroadcastReceiver
public class BootComplementReceiver extends BroadcastReceiver {
private static final String TAG = "BootComplementReceiver";
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "onReceive");
String action = intent.getAction();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.v(TAG, "action = " + action);
}
if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)
|| action.equals(Intent.ACTION_TIME_CHANGED)) {
Log.v(TAG, "action = " + action);
}
}
}
附录1:
android.bluetooth.intent.action.BONDING_CREATED
android.bluetooth.intent.action.BONDING_REMOVED
android.bluetooth.intent.action.DISABLED
android.bluetooth.intent.action.DISCOVERY_COMPLETED
android.bluetooth.intent.action.DISCOVERY_STARTED
android.bluetooth.intent.action.ENABLED
android.bluetooth.intent.action.HEADSET_STATE_CHANGED
android.bluetooth.intent.action.MODE_CHANGED
android.bluetooth.intent.action.NAME_CHANGED
android.bluetooth.intent.action.PAIRING_CANCEL
android.bluetooth.intent.action.PAIRING_REQUEST
android.bluetooth.intent.action.REMOTE_ALIAS_CHANGED
android.bluetooth.intent.action.REMOTE_ALIAS_CLEARED
android.bluetooth.intent.action.REMOTE_DEVICE_CONNECTED
android.bluetooth.intent.action.REMOTE_DEVICE_DISAPPEARED
android.bluetooth.intent.action.REMOTE_DEVICE_DISAPPEARED
android.bluetooth.intent.action.REMOTE_DEVICE_DISCONNECTED
android.bluetooth.intent.action.REMOTE_DEVICE_DISCONNECT_REQUESTED
android.bluetooth.intent.action.REMOTE_DEVICE_FOUND
android.bluetooth.intent.action.REMOTE_NAME_FAILED
android.bluetooth.intent.action.REMOTE_NAME_UPDATED
android.intent.action.AIRPLANE_MODE
android.intent.action.BATTERY_CHANGED
android.intent.action.BATTERY_LOW
android.intent.action.BOOT_COMPLETED
android.intent.action.CAMERA_BUTTON
android.intent.action.CONFIGURATION_CHANGED
android.intent.action.DATA_SMS_RECEIVED
android.intent.action.DATE_CHANGED
android.intent.action.DEVICE_STORAGE_LOW
android.intent.action.DEVICE_STORAGE_OK
android.intent.action.GTALK_CONNECTED
android.intent.action.GTALK_DISCONNECTED
android.intent.action.HEADSET_PLUG
android.intent.action.MANAGE_PACKAGE_STORAGE
android.intent.action.MEDIA_BAD_REMOVAL
android.intent.action.MEDIA_BUTTON
android.intent.action.MEDIA_EJECT
android.intent.action.MEDIA_MOUNTED
android.intent.action.MEDIA_REMOVED
android.intent.action.MEDIA_SCANNER_FINISHED
android.intent.action.MEDIA_SCANNER_SCAN_FILE
android.intent.action.MEDIA_SCANNER_STARTED
android.intent.action.MEDIA_SHARED
android.intent.action.MEDIA_UNMOUNTABLE
android.intent.action.MEDIA_UNMOUNTED
android.intent.action.NEW_OUTGOING_CALL
android.intent.action.PACKAGE_ADDED
android.intent.action.PACKAGE_CHANGED
android.intent.action.PACKAGE_INSTALL
android.intent.action.PACKAGE_REMOVED
android.intent.action.PACKAGE_RESTARTED
android.intent.action.POWER_CONNECTED
android.intent.action.POWER_DISCONNECTED
android.intent.action.PROVIDER_CHANGED
android.intent.action.REBOOT
android.intent.action.SCREEN_OFF
android.intent.action.SCREEN_ON
android.intent.action.TIMEZONE_CHANGED
android.intent.action.TIME_SET
android.intent.action.TIME_TICK
android.intent.action.UID_REMOVED
android.intent.action.UMS_CONNECTED
android.intent.action.UMS_DISCONNECTED
android.intent.action.WALLPAPER_CHANGED
android.media.RINGER_MODE_CHANGED
android.media.VIBRATE_SETTING_CHANGED
android.net.wifi.NETWORK_IDS_CHANGED
android.net.wifi.RSSI_CHANGED
android.net.wifi.SCAN_RESULTS
android.net.wifi.STATE_CHANGE
android.net.wifi.WIFI_STATE_CHANGED
android.net.wifi.supplicant.CONNECTION_CHANGE
android.net.wifi.supplicant.STATE_CHANGE
android.provider.Telephony.SIM_FULL
android.provider.Telephony.SMS_RECEIVED
android.provider.Telephony.WAP_PUSH_RECEIVED
第二种方法:
1. 继承BroadcastReceiver,实现onReceive方法。
2. 注册该BroadcastReceiver。
3. 最后销毁该BroadcastReceiver。
分享到:
相关推荐
BroadCastReceiver
BroadcastReceiver,两种注册方式
android的broadcastReceiver的介绍。
地址:http://blog.csdn.net/qq_26787115/article/details/51113053
Android \BroadCastReceiver 最全的简介
BroadcastReceiver学习(01)
android BroadcastReceiver相关测试代码。我测试所用的。
android BroadcastReceiver 源码 学习
以获取电池电量为例对BroadcastReceiver和接口进行了简单讲解
Android学习之BroadcastReceiver总结
演示了BroadcastReceiver的简单用法。
Android下BroadcastReceiver的简单示例。详见博客:http://www.cnblogs.com/plokmju/p/android_BroadcastReceiver.html
BroadcastReceiver应用详解 BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播。
这是一个关于Android中四大组件之一 —— BroadcastReceiver,即广播的一个demo,里面涉及到BroadcastReceiver的两种注册方式,也简单的实现了广播,通过点击按钮发送广播,收到广播后打印日志。
Android-BroadcastReceiver高级应用,详细参见博客:http://www.cnblogs.com/plokmju/p/android_SystemBroadcastReceriver.html
介绍BroadcastReceiver使用的demo,具体的使用方法可以参看我的博客
这是一个国外的android手机应用源码实例,BroadcastReceiver这个应用我还没用过,不知道是干什么的,源码可为学习安卓开发的做些参考吧。
对于这样的BroadcastReceiver,通常是在产生某个特定的系统事件之后,进行相应的操作,比如说wifi网卡打开时,给用户一个提示;ii. 第二种注册方法相对第一种要灵活的多,这样注册的BroadcastReceiver通常用于更新UI...
Android BroadcastReceiver