0

BootReceiver.kt

override fun onReceive(context: Context, intent: Intent) {
    if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
        for (i in 0 .. 10) {
            Toast.makeText(context, "number $i", Toast.LENGTH_SHORT).show()
            Thread.sleep(1000)
        }
    }
}

Manifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application>

    <receiver android:name=".BootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

If the mobile phone is rebooted in the above code, the for loop does not run. By any chance, when ACTION_BOOT_COMPLETED inside the onReceive() function, when I just opened Toast, it was executed.

ex)

if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
    Toast.makeText(context, "Reboot", Toast.LENGTH_SHORT).show()
}

Why is the for statement not executed?? How can I execute the for statement?? If you look at the code, the reason for putting Thread.sleep(1000) is that Toast might pass too quickly. For reference, removing Thread.sleep(1000) did not execute the for statement.

Wowo Ot
  • 1,362
  • 13
  • 21
H.JiMan
  • 269
  • 1
  • 2
  • 10
  • is it working without Thread.sleep(1000) ? – NehaK Oct 26 '20 at 07:44
  • Even without ```Thread.sleep(1000)``` the for statement doesn't work. – H.JiMan Oct 26 '20 at 07:47
  • may be this method it self it not getting called.. try this https://stackoverflow.com/questions/20441308/boot-completed-not-working-android – NehaK Oct 26 '20 at 07:55
  • Thank you for your advice. After ACTION_BOOT_COMPLETED, startService was started, but ```Emulator: qemu: unsupported keyboard cmd=0x84``` error occurs and the service is not running. – H.JiMan Oct 26 '20 at 08:18

0 Answers0