API 27无法接收BOOT_COMPLETED操作

时间:2017-12-20 14:40:21

标签: java android-8.1-oreo

下面的代码虽然适用于API 25,但未通过API 27接收BOOT_COMPLETED操作。

然而,根据the official documentation,此操作属于隐式广播延迟。

当我输入am broadcast -a android.intent.action.BOOT_COMPLETED的adb命令时,控制台上会显示以下信息:

Background execution not allowed: receiving Intent { act=android.intent.action.BOOT_COMPLETED flg=0x400010 } to com.boottest/.OnBootReceiver

有没有机会克服这个问题?

我的AndroidManifest.xml文件:

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

<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="27" />

<application ... >
    ....
    <receiver android:name=".OnBootReceiver" android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>
....

我的build.gradle文件:

...
android {
    compileSdkVersion 27
    buildToolsVersion "27.0.1"

    defaultConfig {
        applicationId "com.boottest"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    ...
}

dependencies {
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:27.0.+"
    compile "com.facebook.react:react-native:+"  // From node_modules
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

我的OnBootReceiver.java文件:

package com.boottest;

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.d("BootTest", " OnBootReceiver - Received a broadcast!");
  }
}

1 个答案:

答案 0 :(得分:1)

问题是Android模拟器。

使用Genymotion时,它按预期工作,OnBootReceiver接收器类在启动时评估BOOT_COMPLETED操作。