BOOT_COMPLETED没有使用Android

yah*_*hya 43 android broadcastreceiver bootcompleted

首先,我知道已经有数百个问这样的问题了,但是我已经检查了一段时间,但仍然找不到任何解决方案.

我已经看到这个答案说BOOT_COMPLETED没有发送到应用程序,除非用户首先启动你的应用程序,在Android版本3.1之后但我仍然看到一些应用程序正在这样做,必须有一种方法.我真的需要处理它,否则我也反对在没有用户交互的情况下做某事.

所以这是我的AndroidManifest:

<manifest ... >

<!-- to be activated service on boot is completed -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application ... >
    <!-- to receive data when boot completed -->
    <receiver
        android:name="myPackage.BootReceiver"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

提前致谢.

编辑:在我的广播接收器中没有太多东西要看,但是这里需要的是:

package myPackage
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Utils.LogI("BootReceiver", "BootReceiver received!");
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
        // Do my stuff
    }
}
}
Run Code Online (Sandbox Code Playgroud)

Piy*_*ush 69

以下这件事对我有用

AndroidManifest.xml中

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

<application>    

    <receiver android:name=".BootCompletedReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>

    <service android:name="NotifyingDailyService" >
    </service>
Run Code Online (Sandbox Code Playgroud)

BootCompletedReceiver.class

public class BootCompletedReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
    // TODO Auto-generated method stub
    Log.w("boot_broadcast_poc", "starting service...");
    context.startService(new Intent(context, NotifyingDailyService.class));
}

}
Run Code Online (Sandbox Code Playgroud)

Service.class

 public class NotifyingDailyService extends Service {

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent pIntent, int flags, int startId) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "NotifyingDailyService", Toast.LENGTH_LONG).show();
    Log.i("com.example.bootbroadcastpoc","NotifyingDailyService");

    return super.onStartCommand(pIntent, flags, startId);
}
}
Run Code Online (Sandbox Code Playgroud)

  • 当您使用 targetAndroidSDK 27 以上构建 Android 时,它将不起作用。 (3认同)
  • @KarueBensonKarue 您需要在安装后至少打开一次您的应用程序。或者您可能不小心“强制停止”它,从而阻止接收广播。 (2认同)
  • 这将不再适用http://stackoverflow.com/a/19856367/770467(3.1+) (2认同)
  • 我检查了您引用的链接,也许您对此有所误解。它是“应用程序”而不是“活动”的停止状态。*应用程序**首次安装但尚未启动**且由用户手动停止(在“管理应用程序”中)*时处于停止状态。*这意味着,用户应在安装后至少启动一次应用程序以激活该应用程序,然后该应用程序可以正常接收来自OS的所有隐式广播。* (2认同)

Thi*_*nce 34

这是一个古老而基本的问题,但很多Android开发人员现在仍然对这个问题感到困惑,因为他们没有时间仔细阅读文档

我看到有人分享了一些链接并说: "这将不再适用",这是完全错误误解.

关于这个问题:"我看到这个答案说BOOT_COMPLETED没有发送到应用程序,除非用户在Android 3.1版之后首先启动你的应用程序",请阅读这些内容(来自官方文档:https://developer.android. com/about/versions/android-3.1.html #initartcontrols)正确理解:

  • 请注意,应用程序的停止状态Activity的停止状态不同.系统分别管理这两个停止的状态.

  • 应用程序在首次安装但尚未启动时以及用户手动停止时(在" 管理应用程序"中)处于停止状态.(他们的意思是强制停止应用)

在此输入图像描述

  1. 这意味着用户应该在安装后至少启动应用程序一次以激活应用程序,然后应用程序可以正常接收来自操作系统的隐式广播.(只有一次发射!)

  2. "是否有任何应用程序安装,甚至从未打开过一次?" ,是的,它是垃圾邮件和诈骗应用程序,这种技术可以帮助用户防止这种情况!

FURTHERMORE,直到现在(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>
Run Code Online (Sandbox Code Playgroud)

最后,请仔细阅读文档并仔细思考一次代码:3!

  • 我在 Orio 中测试过不工作。不过信息非常丰富,谢谢 (2认同)
  • 这与电池优化有关。如果您将应用程序从电池优化中删除,它就会起作用 (2认同)

小智 8

对于像我一样仍然遇到此问题的其他人,如果您在具有启动锁(引脚、图案或其他)的设备上进行调试,则操作系统版本 >= 7.0 需要订阅如下所示android.intent.action.LOCKED_BOOT_COMPLETED

<receiver
    android:directBootAware="true"
    android:name=".BootCompletedReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT" />
        <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

您可以在以下链接找到文档:https ://developer.android.com/training/articles/direct-boot


ima*_*yni 6

一些新的平板电脑和安卓设备默认有安全应用程序。有时这些应用程序会锁定您的自动启动模式。这个安全应用程序的一个例子是 MyAsus manager。所以你可以在你的应用程序中添加“允许自动启动”

  • 惠威手机的情况也是如此。BOOT_COMPLETED 未触发。要使其触发,您作为用户应该转到“设置”-&gt;“应用程序”-&gt;“应用程序启动”-&gt; 关闭所需的应用程序。 (3认同)

Xar*_*mer 6

并为Htc设备添加com.htc.intent.action.QUICKBOOT_POWERON

    <receiver android:enabled="true" android:name=".receivers.BootUpReceiver">
        <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"/>
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
         </intent-filter>
    </receiver>
Run Code Online (Sandbox Code Playgroud)


小智 6

问题出在设备上。某些设备仅允许内部应用接收此操作(例如:Android 5.1)。

您可以将其添加到您的意图过滤器中作为解决方法

行动 android:name="android.intent.action.USER_PRESENT"

这是在用户解锁设备后触发的。