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.