Scheduling alarm every 2 minutes android

末鹿安然 提交于 2019-12-05 02:24:56
sam

In your setRepeating function, you should use SystemClock.elapsedRealTime() for ELAPSED_REALTIME_WAKEUP. Also, you need to change 2000 to 2*60*1000 to specify your interval time.

alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                          SystemClock.elapsedRealtime(),
                          2*60*1000, 
                          pendingIntent);

Hope this helps.

Reference: ELAPSED_REALTIME_WAKEUP

EDIT: In your manifest file, there is a typo in your receiver name. Change ".AlarmReciever" to ".AlarmReceiver".

<receiver
    android:name=".AlarmReceiver"
    android:exported="true" >
</receiver>

in your code you set the alarm this way

alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            System.currentTimeMillis(),
            2000,
            pendingIntent);

the interval time is wrong to run every two minutes you should write:

alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            0,
            1000 * 60 * 2,
            pendingIntent);

EDIT

for your pending intent set flag PendingIntent.FLAG_UPDATE_CURRENT and see if it changes anything.

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