AlarmManager间隔为Android [英] AlarmManager Interval for Android

查看:188
本文介绍了AlarmManager间隔为Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是code我迄今为止

This is the code I have so far

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
setRepeatingAlarm();

public void setRepeatingAlarm() {

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 10);

    Intent intent = new Intent(this, TimeAlarm.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), (15 * 1000), pendingIntent);
  }

}

这是所有我试图做到:警报将无法打开,直到30秒过去的每一分钟。一旦你明确表示,它不会回来,直到30秒过去的下次的分钟。所以,如果我打开应用程序,它是25秒过去分钟,它会激活状态栏通知5秒钟后。但如果为40秒过去,我将不得不等待50秒钟(进入下一分钟)。我不知道如何使用日历功能来实现这一点?

This is all I am trying to accomplish: The alarm will not turn on until 30 seconds past every minute. Once you clear it, it wont comes back on until 30 seconds past the next minute. So if I open the app, and it is 25 second past the minute, it will activate status bar notification 5 second later. But if it is 40 seconds past, I will have to wait 50 more seconds (into the next minute). I am not sure how to use the Calendar function to attain this?

推荐答案

如果我理解你的要求,那么你可以尝试像下面这样...

If I understand your requirement then you could try something like the following...

Calendar cal = Calendar.getInstance();
if (cal.get(Calendar.SECOND) >= 30)
    cal.add(Calendar.MINUTE, 1);
cal.set(Calendar.SECOND, 30);

// Do the Intent and PendingIntent stuff

am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60 * 1000, pendingIntent);

这篇关于AlarmManager间隔为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆