title: Broadcast Receiver Not Working After Device Reboot in Android
body: <p>I have already checked all the related questions and have not found any solution for this problem. So this is an absolutely new problem for me.</p>
  
  <p><strong>What I Have</strong></p>
  
  <p>I have an Android app which registers a few broadcast receivers in its manifest. This is what my manifest looks like.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      package="com.app.myapp"&gt;
  
      &lt;uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /&gt;
      &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
      &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.USE_FINGERPRINT" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.READ_CALL_LOG" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_CALL_LOG" /&gt;
      &lt;uses-permission android:name="com.android.vending.BILLING" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;
  
      &lt;uses-feature
          android:name="android.hardware.telephony"
          android:required="false" /&gt;
  
      &lt;uses-feature
          android:name="android.hardware.screen.portrait"
          android:required="false" /&gt;
  
      &lt;application
          android:name=".base.MyApp"
          android:allowBackup="false"
          android:icon="@drawable/ic_launcher"
          android:label="@string/label_app_name"
          android:largeHeap="true"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"
          tools:replace="label, allowBackup"&gt;
  
          &lt;receiver android:name=".mics.BootReceiver"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
                  &lt;action android:name="android.intent.action.QUICKBOOT_POWERON" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  
          &lt;receiver android:name=".PhoneCallReceiver"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.NEW_OUTGOING_CALL" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  
          &lt;receiver
              android:name=".mics.DeviceAdminReceiver"
              android:permission="android.permission.BIND_DEVICE_ADMIN"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /&gt;
              &lt;/intent-filter&gt;
  
              &lt;meta-data
                  android:name="android.app.device_admin"
                  android:resource="@xml/device_admin" /&gt;
          &lt;/receiver&gt;
  
          &lt;receiver
              android:name="com.clevertap.android.sdk.InstallReferrerBroadcastReceiver"
              android:exported="true"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="com.android.vending.INSTALL_REFERRER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  
          &lt;meta-data
              android:name="com.app.myapp.utils.ImageLoaderModule"
              android:value="GlideModule" /&gt;
  
          &lt;meta-data
              android:name="com.app.myapp.utils.AudioCoverLoaderModule"
              android:value="GlideModule" /&gt;
  
          &lt;provider
              android:name="android.support.v4.content.FileProvider"
              android:authorities="${applicationId}.provider"
              android:exported="false"
              android:grantUriPermissions="true"&gt;
  
              &lt;meta-data
                  android:name="android.support.FILE_PROVIDER_PATHS"
                  android:resource="@xml/provider_paths" /&gt;
          &lt;/provider&gt;
  
          &lt;activity
              android:name=".core.activities.SplashActivity"
              android:excludeFromRecents="true"
              android:label="@string/label_app_name"
              android:screenOrientation="portrait"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;activity-alias
              android:name=".core.activities.SplashActivity-Alias"
              android:icon="@drawable/ic_launcher"
              android:label="@string/label_app_name"
              android:noHistory="true"
              android:targetActivity="com.app.myapp.core.activities.SplashActivity"&gt;
  
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
                  &lt;category android:name="android.intent.category.MONKEY" /&gt;
              &lt;/intent-filter&gt;
  
          &lt;/activity-alias&gt;
  
          &lt;activity
              android:name=".core.flow.authFlow.activities.AuthFlowActivity"
              android:excludeFromRecents="true"
              android:label="@string/label_app_name"
              android:screenOrientation="portrait" /&gt;
  
          &lt;service android:name=".features.fileCloudSync.KillNotificationService" /&gt;
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>There are 10-15 other activities as well but have been removed for simplicity. And this is the basic boot receiver class. I start a service from here.</p>
  
  <pre><code>public class BootReceiver extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
              AlertUtils.showToast(context, "BOOT COMPLETED", Toast.LENGTH_LONG);
          }
      }
  }
  </code></pre>
  
  <p>and the phone call receiver class looks something like this (it has been simplified as well),</p>
  
  <pre><code>public class PhoneCallReceiver extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
              AlertUtils.showToast(context, "PHONE CALL RECEIVED", Toast.LENGTH_LONG);
              // Simplified for brevity
          }
      }
  }
  </code></pre>
  
  <p><strong>The Problem</strong></p>
  
  <p>All these receivers work fine when I install the app and start it once. But after I reboot my device these receivers don't work at all. Neither the <code>BootCompleteReceiver</code> nor the <code>PhoneCallReceiver</code> gets their <code>onReceive()</code> method called.</p>
  
  <p>My assumption was that these receivers would get registered automatically after reboot, but it just doesn't work. I need the <code>BootCompleteReceiver</code> to work so that I can start an important service in my app.</p>
  
  <p><strong>My Observations</strong></p>
  
  <p>I have tested this thoroughly. After rebooting the device, the receivers work fine in my <strong>Nexus 5X (Nougat), Nexus 6P (Nougat), YU Yuphoria (Lollipop)</strong> but not in my <strong>OnePlus 3 (Nougat) and Mi 4i (Lollipop)</strong>.</p>
  
  <p>How can the same code work perfectly on a few devices and not work at all on the other devices? I haven't changed anything at all.</p>
  
  <p>What am I doing wrong here? My app is heavily dependent on these broadcasts and starts services based on these. Any help will be highly appreciated.</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>To understand the problem better, I just created a very small test project with just a single activity and the exact same <code>BootCompleteReceiver</code> and <code>PhoneCallReceiver</code>.</p>
  
  <p>But weirdly, this project works perfectly on my OnePlus 3 where my actual app's receivers don't work after a reboot. I was initially assuming that the problem is in the OS or the device somehow, but it is not.</p>
  
  <p>So where is the actual problem? Is it in my app (but it works perfectly on other devices) or in the OS and device (the small test project works fine on the same OS and same device)?</p>
  
  <p>It is really confusing to me. I would need some expert help on this.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I have tried the suggestion given by @shadygoneinsane. Here are my observations.</p>
  
  <p>1) I tried to send the BOOT_COMPLETED broadcast via ADB.</p>
  
  <pre><code>./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -p com.app.myapp
  </code></pre>
  
  <p>And I got this stack trace,</p>
  
  <pre><code>Broadcasting: Intent { act=android.intent.action.BOOT_COMPLETED pkg=com.app.myapp }
  java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.BOOT_COMPLETED from pid=25378, uid=2000
      at android.os.Parcel.readException(Parcel.java:1683)
      at android.os.Parcel.readException(Parcel.java:1636)
      at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:3696)
      at com.android.commands.am.Am.sendBroadcast(Am.java:778)
      at com.android.commands.am.Am.onRun(Am.java:404)
      at com.android.internal.os.BaseCommand.run(BaseCommand.java:51)
      at com.android.commands.am.Am.main(Am.java:121)
      at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
      at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:276)
  </code></pre>
  
  <p>Maybe because my device is not rooted. I am unable to send this broadcast in any way.</p>
  
  <p>2) I tried with the PROCESS_OUTGOING_CALLS broadcast after that.</p>
  
  <pre><code>./adb shell am broadcast -a android.intent.action.PROCESS_OUTGOING_CALLS -p com.app.myapp
  </code></pre>
  
  <p>I got this,</p>
  
  <pre><code>Broadcasting: Intent { act=android.intent.action.PROCESS_OUTGOING_CALLS pkg=com.app.myapp }
  Broadcast completed: result=0
  </code></pre>
  
  <p>It seems that the broadcast was successful, but I do not see any Toast or any log. I then opened my dialer to dial a number and I can then see the Toast and the log both.</p>
  
  <p><strong>So it seems that sending the broadcast via ADB didn't work</strong>, but actually opening the dialer and dialing a number did. </p>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>As per the suggestion from @ChaitanyaAtkuri, I have also tried adding priority to the intent-filters but that didn't work as well.</p>
  
  <p>I have used priorities like 500, 999 and even the highest integer value, but nothing works. This problem is also occurring in some of my friends apps as well. They work in some devices and doesn't work in others.</p>
  
  <p><strong>EDIT 4</strong></p>
  
  <p>I have <strong>finally found out the root cause of the problem happening in my OnePlus 3</strong>. My OnePlus 3 recently got updated to Nougat and they introduced a feature similar to Mi devices which prevent certain apps from auto-starting after reboot.</p>
  
  <p>Upon disabling this feature my app started receiving broadcasts after reboot perfectly. But this still doesn't explain two things.</p>
  
  <p>1) My small test project is whitelisted automatically in the list of AutoLaunch apps and that is why it works as expected. But how is this possible? Why the OS considers this small app worthy to be auto-started?</p>
  
  <p>2) There are some apps like LockDown Pro, 500 Firepaper which is blacklisted in the AutoLaunch apps screen but still, it receives broadcasts after reboot in my OnePlus 3 and Mi 4i. How is that possible now? Is it somehow possible to programmatically allow my app to auto launch in these devices (OnePlus and Mi)?</p>
  
  <p><strong>EDIT 5</strong></p>
  
  <p>I have tried the solution proposed by @Rahul Chowdhury and it really seems to work very well. After adding the accessibility service the problem is re-solved.</p>
  
  <p>But if the user revokes the accessibility permission after granting it then is there a way for me to programmatically check if the accessibility permission is available to my app?</p>