[英]Android BroadcastReceiver not receiving BOOT_COMPLETED event
I have created in my application a BroadcastReceiver that receives the BOOT_COMPLETED event ( BootReceiver
) and then start a service ( NtService
) this service have a public static boolean ( started
) that is setted to true by his onCreate()
method but when i print the var to the console in the MainActivity the boolean is still false. 我在应用程序中创建了一个BroadcastReceiver ,它接收BOOT_COMPLETED事件(
BootReceiver
),然后启动一个服务( NtService
),该服务具有一个公共静态布尔值( started
),该布尔值由他的onCreate()
方法设置为true,但是当我打印var到MainActivity中的控制台,布尔值仍然为false。
The application is installed in Internal Storage ,and i'm debugging it in the android studio emulator by submitting this command on adb shell: 该应用程序安装在Internal Storage中 ,并且我通过在adb shell上提交以下命令在android studio仿真器中对其进行调试:
am broadcast -a android.intent.action.BOOT_COMPLETED
Here is the code: 这是代码:
BootReceiver 引导接收器
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, NtService.class);
context.startActivity(i);
}
}
NtService 网络服务
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class NtService extends Service {
public static boolean started;
public NtService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate(){
started=true;
}
}
AndroidManifest Android清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.primerdime.cloudchat">
<!-- PERMISSION -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="false"
android:icon="@drawable/icon"
android:installLocation="internalOnly"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".registration" />
<!-- SERVICE AND RECEIVER -->
<service
android:name=".NtService"
android:enabled="true"
android:exported="false" />
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action._BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Remove android:exported="false"
from the <receiver>
element. 从
<receiver>
元素中删除android:exported="false"
。 As it stands, it cannot receive broadcasts from outside of the app. 就目前而言,它无法接收来自应用外部的广播。
Also, you have a typo in the <action>
element. 另外,您在
<action>
元素中有一个错字。 It should be <action android:name="android.intent.action.BOOT_COMPLETED" />
. 它应该是
<action android:name="android.intent.action.BOOT_COMPLETED" />
。
And, finally, replace context.startActivity(i);
最后,替换
context.startActivity(i);
with context.startService(i);
与
context.startService(i);
, as you are trying to start a service, not an activity. ,因为您尝试启动服务而非活动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.