BOOT_COMPLETED
下面的代码虽然适用于 API 25,但不会收到API 27 的操作。
但是,根据官方文档,此操作属于隐式广播例外。
当我输入 adb 命令时am broadcast -a android.intent.action.BOOT_COMPLETED
,控制台上会显示以下消息:
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!");
}
}