第一种方法:

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。

更多相关文章

  1. Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明
  2. Android异步加载图像小结 (含线程池,缓存方法)
  3. android上一些方法的区别和用法的注意事项
  4. Android实现下载文件功能的方法
  5. Android 通讯录中用到的关于按名字首字母排序方法
  6. 开机引导程序只执行一次的方法
  7. Android MediaPlayer 常用方法介绍
  8. Android 自定义组件 方法篇

随机推荐

  1. android定义可以被其他程序调用的activit
  2. 定制Android之4G-LTE模块驱动
  3. 【Android】数据存储之Files
  4. android中从图库中选取图片
  5. Android动画学习笔记-Android(安卓)Anima
  6. Android(安卓)NDK概述
  7. Android视频开发国人开发的Vitamio开源项
  8. android学习之LinearLayout
  9. Android局部布局替换实现
  10. android粗略获得程序运行时间的方法