1. 说明

Android手机开机后,会发送android.intent.action.BOOT_COMPLETED广播,监听这个广播就能监听开机。


2. 代码

<uses-permissionandroid:name="android.permission.RECEIVE_BOOT_COMPLETED"/><receiverandroid:name="com.example.restarttest.BootupReceiver"><intent-filter><actionandroid:name="android.intent.action.BOOT_COMPLETED"/></intent-filter></receiver>

publicclassBootupReceiverextendsBroadcastReceiver{privatefinalStringACTION_BOOT="android.intent.action.BOOT_COMPLETED";@OverridepublicvoidonReceive(Contextcontext,Intentintent){if(ACTION_BOOT.equals(intent.getAction()))Toast.makeText(context,R.string.bootup_receiver,Toast.LENGTH_SHORT).show();}}


3. 问题

Android API Level8以上的时候,程序可以安装在SD卡上。如果程序安装在SD卡上,那么在BOOT_COMPLETED广播发送之后,SD卡才会挂载,因此程序无法监听到该广播。

4. 解决

监听SD卡的挂载。


5. 同时监听的Intent-Filter问题

如果BOOT_COMPLETED和MEDIA_MOUNTED,MEDIA_EJECT写在同一个intent-filter中,那么无法检测到BOOT_COMPLETED,对于没有SD卡的手机,只能检测BOOT_COMPLETED,这样就会导致无法检测到开机了。

<receiverandroid:name=".com.example.restarttest.BootupReceiver"><intent-filterandroid:priority="1000"><actionandroid:name="android.intent.action.BOOT_COMPLETED"/></intent-filter><intent-filterandroid:priority="1000"><actionandroid:name="android.intent.action.MEDIA_MOUNTED"/><actionandroid:name="android.intent.action.MEDIA_EJECT"/><dataandroid:scheme="file"/></intent-filter></receiver>



更多相关文章

  1. Android6.0 MTK 需求文档(五)
  2. Android(安卓)监听 home键和菜单键
  3. Android(安卓)Studio 启用Lambda表达式
  4. 去掉非首次开机的优化过程都会有'Android正在升级'的提示
  5. Android摇一摇
  6. Android(安卓)之NavigationView&Toolbar&DrawerLayout
  7. Android(安卓)广播 使用方法
  8. Android(安卓)广播(Android(安卓)10)
  9. android 官方的下拉刷新:SwipeRefreshLayout

随机推荐

  1. golang开发android应用(二) - go语言生成an
  2. android 版本更新和下载安装 适配android
  3. android菜鸟学习笔记16----Android项目打
  4. Android系列之Wifi定位
  5. AsyncTask使用和源码解析
  6. android开发每日汇总【2011-11-10】
  7. android 反编译
  8. 系出名门Android(5) - 控件(View)之TextV
  9. 如何关闭android中的HandlerThread
  10. android 使用Intent传递数据之返回结果