Android BootReceiver does not work

廉价感情. 提交于 2019-12-22 08:26:28

问题


I'm trying to listen for reboot event.

I'v created the following class:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class OnBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MYBOOTRECEIVER", "HELLO!");
    }
}

and then in the manifest file I've putted the following xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mypackage"
    android:installLocation="internalOnly"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application android:label="@string/app_name" >
        <receiver android:name=".OnBootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

However, after I've installed this app and rebooted the device, nothing happens. (I'm using a galaxy tab 8.9 with android 3.2 on it). As you can see from the manifest I've installed the app on internal memory (like suggested on similar questions here on stackoverflow) ... I've also putted a Quickboot_poweron action (to see if galaxy tab have similar behaviour of htc devices) ... but nothing. I hope that someone can help me!


回答1:


I faced same problem earlier on post 3.0 device.

Reason is

Starting with 3.1 when applications are installed they are in a “stopped” state so they will not be able to run until the user explicitly launches them. Pressing Force Stop will return them to this state.

once the user runs the app for the first time (and does not Force Stop it), everything behaves as before — a reboot will cause BOOT_COMPLETED broadcasts to be received and so on. However, if the user installs the app, until and unless they run the app manually, no broadcasts will be received.

So in your case you will have to create launcher activity and make sure you start that launcher activity at least once then you will start receive boot event broadcast.




回答2:


The problem is that with Honeycomb and beyond, broadcast signals do not automatically start an application which has not been otherwise started. There is a flag that can be used to change this however the BOOT_COMPLETED broadcast does not carry that flag. You need to be installed on the system partition in order to overcome this nuance.

If you're not able to be installed on the system partition, you'll need to find some other way to be started -- either by convincing the user to start you manually, or having the user install a widget you provide, etc.




回答3:


The action mention in your manifest is correct except the category part, this can be exclude if you just want to receive for boot complete. And in Receiver file this will work fine, but you should always use an if-else statement to distinguish between actions received.

    if (action.equals(android.intent.action.BOOT_COMPLETED)) {
       Log.i("MYBOOTRECEIVER", "BOOT COMPLETED RECEIVED");
    }


来源:https://stackoverflow.com/questions/11098926/android-bootreceiver-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!