Helio
Helio

Reputation: 193

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.

The application is installed in Internal Storage,and i'm debugging it in the android studio emulator by submitting this command on adb shell:

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

<?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>

Upvotes: 0

Views: 934

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006674

Remove android:exported="false" from the <receiver> element. As it stands, it cannot receive broadcasts from outside of the app.

Also, you have a typo in the <action> element. It should be <action android:name="android.intent.action.BOOT_COMPLETED" />.

And, finally, replace context.startActivity(i); with context.startService(i);, as you are trying to start a service, not an activity.

Upvotes: 1

Related Questions