我已經在我的應用程序廣播接收器接收 BOOT_COMPLETED事件(BootReceiver
),然後啓動服務(NtService
)這個服務有一個公共靜態布爾創建BOOT_COMPLETED事件(started
)被設置好的來真的通過他的onCreate()
方法,但是當我在MainActivity打印var到控制檯時,布爾值仍然是錯誤的。Android的廣播接收器接收不到
的應用程序安裝在內部存儲,我通過亞行殼提交此命令調試它在Android模擬器工作室:
am broadcast -a android.intent.action.BOOT_COMPLETED
下面是代碼:
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);
}
}
Nt個服務
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
<?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>
我已經刪除了'android:exported =「false」'並更正了''中的錯字,但仍然不起作用 –
Helio
@Helio:查看更新後的答案。 – CommonsWare
完美的工作謝謝。 – Helio