0

I have implemented RECEIVE_BOOT_COMPLETED by making an entry in manifest and implementing MyBootReceiver.onReceive(..).

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
....
<receiver
            android:name=".MyBootReceiver"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
            </intent-filter>
        </receiver>

It works well when run on an emulator (Pixel 2 API 21). However, when it is run on a API Level 30 or on PHysical device, OnePlus 6 (Oxygen 10.3.7), the BOOT notification isn't received.

But, when phone is restarted, other applications like Whatsapp, Sms etc are able to receive messages from server, possibly using some notification event. How is that possible ?

Am I missing something ?

SimpleGuy
  • 2,764
  • 5
  • 28
  • 45

1 Answers1

0

one plus and Mi have customized their OS on top of Android. So they have overriden this function. refer Broadcast Receiver Not Working After Device Reboot in Android

On newer Android versions, you need to run adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -p com.mypackage.name. Without restricting the broadcast to your app, your device will actually reboot

regd your question,

But, when phone is restarted, other applications like Whatsapp, Sms etc are able to receive messages from server, possibly using some notification event. How is that possible ?

It's also possible by accessing the Phone Network State

Mark
  • 7,446
  • 5
  • 55
  • 75
  • When I run the abd command given by you, I get below error: `adb -s emulator-5554 shell am broadcast -a android.intent.action.BOOT_COMPLETED -p com.xxxx Broadcasting: Intent { act=android.intent.action.BOOT_COMPLETED flg=0x400000 pkg=com.filesuploader } Exception occurred while executing 'broadcast': java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.BOOT_COMPLETED from pid=4995, uid=2000` – SimpleGuy Dec 07 '20 at 04:24
  • How would accessing phone network state help when app is not running since device rebooted ? – SimpleGuy Dec 07 '20 at 04:25
  • may I know what you are doing inside your MyBootReceiver. If you are running a service then background execution limits are applicable fromOreo onwards https://developer.android.com/about/versions/oreo/background – Sethu Vignesh Dec 07 '20 at 05:53
  • I now think, Whatsapp etc use JobScheduler / WorkerProcess – SimpleGuy Dec 07 '20 at 08:48
  • Yes, I suggest you add logs in broadcast receivers, to check whether you are actually receiving the broadcast or not. and then try to use foreground service/ workmanger to serve the purpose – Sethu Vignesh Dec 07 '20 at 08:56
  • Thanks. I checked that I am getting BOOT broadcast event but activity is not started in that. So I guess I'll have to do with NotificationManager – SimpleGuy Dec 07 '20 at 11:57
  • Can I use foreground service/ workmanger to serve the purpose to show activity window to user ? Can these worker / service launch activity window ? – SimpleGuy Dec 08 '20 at 04:58
  • you can directly launch activity from broadcst receiver,https://stackoverflow.com/questions/38815216/trying-to-start-an-activity-from-broadcast-receiver/38815374 – Sethu Vignesh Dec 09 '20 at 02:07
  • launching activity is not permitted from Android 8+, 10+ when app is not running. – SimpleGuy Dec 09 '20 at 02:31
  • got it, the ideal approach as per the guidelines is show notification. https://developer.android.com/guide/components/activities/background-starts – Sethu Vignesh Dec 09 '20 at 02:58
  • showing activity from background is not recommended as per Android guidelines, you better show notification to the user and let the user decide to launch the activity/ not. But yet there are some exceptions, using that you can directly launch the app https://developer.android.com/guide/components/activities/background-starts#exceptions – Sethu Vignesh Dec 09 '20 at 03:03