Android手机开机后,会发送android.intent.action.BOOT_COMPLETED广播,监听这个广播就能监听开机。
一般的步骤如下:
1、注册广播
<receiver android:name="com.netmoon.broadcast.BootBroadCastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
2、添加权限
<uses-permission android:name=
"android.permission.RECEIVE_BOOT_COMPLETED"
/>
实现Receiver
public class BootRroadCastReceiver extends BroadcastReceiver {
private final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
if(ACTION_BOOT.equals(intent.getAction()))
Toast.makeText(context, R.string.bootup_receiver, Toast.LENGTH_SHORT).show();
}
}
但是Android API Level8 以上的时候,程序可以安装在SD卡上。如果程序安装在SD卡上,那么在BOOT_COMPLETED广播发送之后,SD卡才会挂载,因此程序无法监听到该广播。
解决办法:同时监听开机和sd卡挂载。(也不能只监听挂载就认为开机了,因为有的手机没有sd卡)
实现对挂载进行监听media mounted如下:
<receiver android:name=".Ge">
<intent-filter >
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<data android:scheme="file">
</data>
</intent-filter>
</receiver>
监听media mounted的Receiver就不写了。
如何实现一个能同时监听开机BOOT_COMPLETED和挂载media mounted的广播接收器呢?
理论上只要将media mounted的intent-filter
和BOOT_COMPLETED的intent-filter
放在一起就行了,但是,放同一个intent-filter里,boot complete监听不到,需要放到两个intent filter中:
<receiver android:name=".Ge">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter >
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<data android:scheme="file">
</data>
</intent-filter>
</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.添加以下:
<receiver android:name=".BootBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
5.代码部分:
public class BootBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//接收广播:系统启动完成后运行程序
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent ootStartIntent = new Intent(context, Login_Activity.class);
ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(ootStartIntent);
}
//接收广播:安装更新后,自动启动自己。
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED) || intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED))
{
Intent ootStartIntent = new Intent(context, Login_Activity.class);
ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(ootStartIntent);
}
}
}
查看一下系统中是否有一类似360管家的软件、他们会默认将一些开机广播给屏蔽掉、加快开机速度、只需将其打开即可。