android接收开机广播

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


监听开机广播步骤:

申请权限

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

注册广播广播

<receiver

            android:name="com.wisdom.service.MBootBroadcastReceiver">

           <intent-filter >

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

            </intent-filter>

</receiver>


代码生命广播

/**

 * 开机广播

 * 

 */

public class MBootBroadcastReceiver extends 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

            android:name="com.wisdom.service.MBootBroadcastReceiver">

            <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_EJECT" />


                <data android:scheme="file" />

            </intent-filter>

        </receiver>


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


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

appledeMacBook-Pro:~ apple$ adb devices
List of devices attached 
040ABGTKS7MC	device

appledeMacBook-Pro:~ apple$ adb -s 040ABGTKS7MC  shell am broadcast -a android.intent.action.BOOT_COMPLETED
Broadcasting: 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:

  1. Install your app to internal storage
  2. Instal 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.


参考地址:

         http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on-boot-on-android


后续再补充。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值