首页 > 解决方案 > 如何修复“启动完成不是通过电话调用而是在模拟器上工作”

问题描述

我试图在手机启动时启动我的应用程序。它在我的模拟器上完美运行(5.4 FWVGA API 27)但在我的华为 p30 pro 上它不工作。

在模拟器上,它启动并返回 Log.e,但在手机上,应用程序没有在后台启动,也没有返回 Log.e

对我来说,如果它只是华为的问题,这没问题,但它让我担心它在其他手机上也不起作用。

我在互联网上遵循了一些教程,但似乎没有任何效果。我尝试为我的应用禁用省电功能,但也没有用。添加额外的 Log.e 但它们永远不会被调用。强制关闭应用程序并重新启动,但应用程序保持关闭状态。完全删除应用程序并重新安装。没有效果。

允许:

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

接收者:

   <receiver
        android:name=".service.BootDeviceReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
        </intent-filter>
    </receiver>

引导设备接收器:

public class BootDeviceReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("AppTest ", "1");
    }
}

我希望它开始输出一些东西。我知道已经有多个关于它的主题,但我发现的几个主题都没有帮助我。

标签: android

解决方案


BOOT_COMPLETED 不会发送到应用程序,除非用户首先启动您的应用程序,在 Android 版本 3.1 之后“,请阅读这些行(来自官方文档:https ://developer.android.com/about/versions/android-3.1.html#发射控制

此外,直到现在 (Android Oreo 8.0),当 Android 限制在 Manifest ( https://developer.android.com/about/versions/oreo/background.html#broadcasts ) 中注册隐式广播时,目前仍有几个广播不受这些广播的限制限制。BOOT_COMPLETED 是他们提到的第一个!(https://developer.android.com/guide/components/broadcast-exceptions.html

顺便说一句,您可以尝试一次

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

<receiver android:name=".BootReceiver" android:enabled="true" android:exported="true">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT"/>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
                <!--For HTC devices-->
                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
            </intent-filter>
        </receiver>

推荐阅读