0

I have created a BroadcastReceiver Class but it is not being triggered by my intent. The BroadcastReceived and intent relationship is registered in the Manifest file but it seems that I am doing something wrong. Is my code set up in the incorrect way? Does the BOOT intent get triggered when the phone boots or is it when the app boots?

BroadcastReceived Class:

public class NetworkSchedulerReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("receiver","test receiver"); // ----------> this doesn't log :(

        JobInfo networkJob = new JobInfo.Builder(0, new ComponentName(context, NetworkSchedulerService.class))
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .setPersisted(true)
                .build();

        JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
        scheduler.schedule(networkJob);
    }
}

Manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hmexperiment">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:allowBackup="false"
      android:theme="@style/AppTheme">
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
          android:launchMode="singleTop"
          android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
          <intent-filter>
              <action android:name="android.intent.action.VIEW" />
              <category android:name="android.intent.category.DEFAULT" />
              <category android:name="android.intent.category.BROWSABLE" />
              <data
                  android:host="ecobee.auth0.com"
                  android:pathPrefix="/android/${applicationId}/callback"
                  android:scheme="${applicationId}" />
          </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
      <service android:name=".NetworkSchedulerService" android:permission="android.permission.BIND_JOB_SERVICE" />
      <receiver android:name=".NetworkSchedulerReceiver">
          <intent-filter>
              <action android:name="android.intent.action.BOOT_COMPLETED" />
          </intent-filter>
      </receiver>
    </application>

</manifest>
udameli
  • 1
  • 2
  • As per my understanding this mechanism works only if we have started application once. If we haven't started application not even once then "BootReceiver" function will not be called. for more have look [this](https://stackoverflow.com/a/17230208/5110595) – Hemant Parmar Sep 11 '18 at 06:05
  • Thank you! Because I never opened the app before rebooting my receiver never got registered. Opening the app and only then rebooting worked. – udameli Sep 13 '18 at 18:30

1 Answers1

0

I have done this by the below mentioned way.

Create your broadcast receiver class first.

class BroadcastReceiverOnBootComplete: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
    if (intent?.action.equals(Intent.ACTION_BOOT_COMPLETED, ignoreCase = true)) {

       // Do something here.
      }
   }
 }

Register your receiver in your AndroidManifeast file like this

 <receiver
        android:name=".receiver.BroadcastReceiverOnBootComplete"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <data android:scheme="package" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <data android:scheme="package" />
        </intent-filter>
    </receiver>

Hope it helps.

sanjay
  • 626
  • 1
  • 7
  • 12