How to repeat an action every day in an Android app?

强颜欢笑 提交于 2019-12-06 11:04:25

1/- This code does not work with REAL DEVICE.

I've downloaded your project, run on my device and it works, it shows the Toast when I click start, and every minute is showing.

I recommend you to take a look on this question

2/- This code does not work if the user restart his device.

If you want to restart your BroadcastReceiver once the device is rebooted or shut downed, you may want to add this code :

Add this in your manifest.xml

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

Create another receiver in manifest.xml

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

And then create a BroadcastReceiver as you did previously for showing the Toast

class BootCompletedIntentReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        if ("android.intent.action.BOOT_COMPLETED" == intent.action) {
          //Start your old broadcastreceiver
        }
    }
}

For more information you can take a look at this post

Hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!