应用程序关闭时未收到Android BOOT_COMPLETED

use*_*443 29 android bootcompleted

我知道这个问题已经在网站上被问了很多,但是,我似乎无法找到解决方案.当应用程序未运行时,不会调用我的BOOT_COMPLETED接收器.

表现:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.startuptest"
    android:versionCode="1"
    android:versionName="1.0"
    android:installLocation="internalOnly">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.startuptest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="com.example.startuptest.StartUpBootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

StartUpBootReceiver:

public class StartUpBootReceiver  extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("startuptest", "StartUpBootReceiver " + intent.getAction());

        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Log.d("startuptest", "StartUpBootReceiver BOOT_COMPLETED");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果应用程序正在运行,我用模拟呼叫

adb shell
am broadcast -a android.intent.action.BOOT_COMPLETED
Run Code Online (Sandbox Code Playgroud)

正确接收事件,但是,如果应用程序关闭,则不会收到事件,也不会在启动时收到事件.

我已经安装了应用程序然后启动它几次以确保它已注册.我很遗憾这个,所以任何建议都会受到高度赞赏.

编辑:我可以在日志中看到所有其他已关闭的应用程序(Youtube,FileObserver等)都收到boot_completed事件,而不是我的.

Can*_*ner 98

Android 3.1开始,所有应用程序在安装时都处于" 停止 "状态.(这与用户从"设置"应用程序强制停止应用程序后应用程序结束的状态相同.)

Android停止状态

处于"已停止"状态时,除了手动启动活动外,应用程序不会以任何理由运行.(意味着没有BroadcastReceviers(ACTION_PACKAGE_INSTALLED,BOOT_COMPLETED等)将被调用,不论为此他们已注册的情况下,直到用户手动运行该应用.)

这是Google的反恶意软件举措.谷歌一直主张用户应该首先从启动器启动一个活动,然后该应用程序可以做很多事情.防止BOOT_COMPLETED在活动启动之前被交付是该论点的逻辑结果.

有关此内容的更多详细信息:http:
//developer.android.com/about/versions/android-3.1.html#launchcontrols
http://commonsware.com/blog/2011/07/05/boot-completed-regression.html
http ://devmaze.wordpress.com/2011/12/05/activating-applications/

  • 如果您的应用程序仅包含“BroadcastReceiver”和“Service”,则必须阅读此答案 (2认同)
  • @Bluesir9 我和你一样困惑,但经过几个小时的阅读后,我有了这些想法。**应用程序在首次安装但尚未启动时以及用户手动停止时(在管理应用程序中)处于停止状态。**这意味着,用户应在安装后至少启动一次应用程序以激活应用程序**,然后应用程序可以正常接收来自操作系统的所有隐式广播。 (2认同)

小智 39

我在BOOT_COMPLETED时启动我的应用程序,所以我知道它正在运行.我添加Log.d它不会显示.我添加Toast它显示.Manifest.xml中的小差异

<receiver android:name="com.example.startuptest.StartUpBootReceiver" android:enabled="true" android:exported="true">
    <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED" />
         <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>            
</receiver>
Run Code Online (Sandbox Code Playgroud)

  • 但这些不是默认值.http://developer.android.com/guide/topics/manifest/receiver-element.html (5认同)
  • 谢谢,这就行了.`android:enabled ="true"android:exported ="true"`也是必需的,所以Android知道这个接收器. (4认同)

Ily*_*man 27

这里的每个答案都添加了一小段信息,所以这里是所有的摘要:

为确保您收到BOOT_COMPLETED,请确保执行以下操作:

  1. 将您的接收器添加到清单(不要忘记标志):

    <receiver android:name="com.yourpacakge.BootReceiver" android:exported="true" android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </receiver>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 添加权限:

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

  3. 安装应用程序后,需要由用户手动至少午餐一次,以便接收启动完成事件.(更多详情)

  • 但是为什么需要默认值? (2认同)