android接收开机广播

之前做了个客户端项目,现在有个问题维护。客户的问题是:推送连接一段时间后就接收不到了。我的第一直觉是推送服务被kill掉了,没有自启,但是后来我发现不是这个原因,,当手机重启后该服务没有启动,也就是说没有响应到开机广播。

监听开机广播步骤:

申请权限

"android.permission.RECEIVE_BOOT_COMPLETED"/>

注册广播广播

<receiver

"com.wisdom.service.MBootBroadcastReceiver">

android:name="android.intent.action.BOOT_COMPLETED"/>

</intent-filter>

</receiver>

代码生命广播

/**

* 开机广播

*

*/

public class MBootBroadcastReceiverextends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

Toast.makeText(context, "开机启动", Toast.LENGTH_LONG).show();

if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {

//…

}

}

}

处理应用安装在sd后监听不到开机广播的问题。

<receiver

"com.wisdom.service.MBootBroadcastReceiver">

android:name="android.intent.action.BOOT_COMPLETED"/>

</intent-filter>

<intent-filter>

android:name="android.intent.action.MEDIA_MOUNTED"/>

android:name="android.intent.action.MEDIA_EJECT"/>

="file"/>

</intent-filter>

此时能同时监听开机广播和sd卡挂载的广播。

使用命令发送开机广播,此时手机会重启。

appledeMacBook-Pro:~ apple$ adb devicesList of devices attached 040ABGTKS7MCdeviceappledeMacBook-Pro:~ apple$ adb -s 040ABGTKS7MC shell am broadcast -a android.intent.action.BOOT_COMPLETEDBroadcasting: Intent { act=android.intent.action.BOOT_COMPLETED }

当手机安装应用后如果重来都没有使用过,则该应用是无法接收到开机广播的。

可以查看下面这句话》

How to start service on device boot(autorun app, etc.)

For first: since version Android 3.1+ you don’t recieve BOOT_COMPLETE if user never started yor app at least once or user "force closed" application. This was done to prevent malware automaticaly register service. This security hole was closed in newer versions of Android.

Solution:

Create app with activity. When user run it once app can recieve BOOT_COMPLETE broadcast message.

For second: BOOT_COMPLETE is sent before external storage is mounted. if app is installed to external storage it won’t receive BOOT_COMPLETE broadcast message.

In this case there is two solution:

Install your app to internal storageInstal another small app in internal storage. This app recieves BOOT_COMPLETE and run second app on external storage.

If your app already installed in internal storage then code below can help you understand how to start service on device boot.

参考地址:

后续再补充。

再怎么风光明媚的自家山川,

android接收开机广播

相关文章:

你感兴趣的文章:

标签云: