Android 无法接收开机广播的问题(SD卡)

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


一般的步骤如下:

1、注册广播

  1. <receiver android:name="com.netmoon.broadcast.BootBroadCastReceiver">  
  2.     <intent-filter>  
  3.         <action android:name="android.intent.action.BOOT_COMPLETED">  
  4.         </action>  
  5.     </intent-filter>  
  6. </receiver>  

2、添加权限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

实现Receiver

  1. public class BootRroadCastReceiver extends BroadcastReceiver {  
  2.     private final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";  
  3.     @Override  
  4.     public void onReceive(Context context, Intent intent) {  
  5.         if(ACTION_BOOT.equals(intent.getAction()))  
  6.             Toast.makeText(context, R.string.bootup_receiver, Toast.LENGTH_SHORT).show();  
  7.     }  
  8. }  

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


解决办法:同时监听开机和sd卡挂载。(也不能只监听挂载就认为开机了,因为有的手机没有sd卡)

实现对挂载进行监听media mounted如下:

  1. <receiver android:name=".Ge">  
  2.     <intent-filter >  
  3.         <action android:name="android.intent.action.MEDIA_MOUNTED"/>  
  4.         <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>  
  5.         <data android:scheme="file">  
  6.         </data>  
  7.     </intent-filter>  
  8. </receiver>  

监听media mounted的Receiver就不写了。

如何实现一个能同时监听开机BOOT_COMPLETED和挂载media mounted的广播接收器呢?

理论上只要将media mounted的intent-filter和BOOT_COMPLETED的intent-filter放在一起就行了,但是,放同一个intent-filter里,boot complete监听不到,需要放到两个intent filter中:

  1. <receiver android:name=".Ge">          
  2.     <intent-filter >  
  3.         <action android:name="android.intent.action.BOOT_COMPLETED"/>  
  4.     </intent-filter>  
  5.     <intent-filter >  
  6.         <action android:name="android.intent.action.MEDIA_MOUNTED"/>  
  7.         <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>  
  8.         <data android:scheme="file">  
  9.         </data>  
  10.     </intent-filter>  
  11. </receiver>  


总结如下:

如下几个原因:
(1)、BOOT_COMPLETED对应的action和uses-permission没有一起添加
(2)、应用安装到了sd卡内,安装在sd卡内的应用是收不到BOOT_COMPLETED广播的
(3)、系统开启了Fast Boot模式,这种模式下系统启动并不会发送BOOT_COMPLETED广播
(4)、应用程序安装后重来没有启动过,这种情况下应用程序接收不到任何广播,包括BOOT_COMPLETED、ACTION_PACKAGE_ADDED、CONNECTIVITY_ACTION等等。
系统为了加强了安全性控制,应用程序安装后或是(设置)应用管理中被强制关闭后处于stopped状态,在这种状态下接收不到任何广播。直到被启动过(用户打开或是其他应用调用)才会脱离这种状态,所以Android3.1之后
(1)、应用程序无法在安装后自己启动
(2)、没有ui的程序必须通过其他应用激活才能启动,如它的Activity、Service、Content Provider被其他应用调用。


但还是有时候收不到的原因呢?

1.安装应用后,首先要启动一次。
2.如果签名后,不可以用eclipse安装apk文件,手动安装好后,也要启动一次。
3.添加以下:
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 <uses-permission android:name="android.permission.RESTART_PACKAGES" />
4.添加以下:

  1. <receiver android:name=".BootBroadcastReceiver" >  
  2.             <intent-filter>  
  3.                 <action android:name="android.intent.action.BOOT_COMPLETED" />  
  4.   
  5.                 <category android:name="android.intent.category.HOME" />  
  6.             </intent-filter>  
  7.             <intent-filter>  
  8.                 <action android:name="android.intent.action.PACKAGE_ADDED" />  
  9.                 <action android:name="android.intent.action.PACKAGE_REMOVED" />  
  10.                 <action android:name="android.intent.action.PACKAGE_REPLACED" />  
  11.   
  12.                 <data android:scheme="package" />  
  13.             </intent-filter>  
  14.         </receiver>  

5.代码部分:
  1. public class BootBroadcastReceiver extends BroadcastReceiver  
  2. {  
  3.     @Override  
  4.     public void onReceive(Context context, Intent intent)  
  5.     {  
  6.         //接收广播:系统启动完成后运行程序  
  7.         if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))  
  8.         {  
  9.             Intent ootStartIntent = new Intent(context, Login_Activity.class);  
  10.             ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  11.             context.startActivity(ootStartIntent);  
  12.         }  
  13.         //接收广播:安装更新后,自动启动自己。        
  14.         if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED) || intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED))  
  15.         {  
  16.             Intent ootStartIntent = new Intent(context, Login_Activity.class);  
  17.             ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  18.             context.startActivity(ootStartIntent);  
  19.         }  
  20.     }  
  21. }  


还有最后一种情况:

查看一下系统中是否有一类似360管家的软件、他们会默认将一些开机广播给屏蔽掉、加快开机速度、只需将其打开即可。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值