[英]AlarmManager doesn't trigger to my service
I set my AlarmManager like below: 我将我的AlarmManager设置如下:
Intent startIntent = new Intent(context, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, startIntent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 3000, pendingIntent);
Log.d(TAG, "Current time is " + System.currentTimeMillis());
Log.d(TAG, "Next alarm time is: " + (alarmManager.getNextAlarmClock().getTriggerTime() - System.currentTimeMillis()));
It shows 表明
Current time is 1499909678520 当前时间是1499909678520
Next alarm time is: 55521479 下一个闹钟时间是:55521479
I thought RTC is UTC time using the System.currentTimeMillis(). 我以为使用System.currentTimeMillis()的RTC是UTC时间。 Should next alarm be a number less than 3000?
下一个警报应该小于3000吗?
FYI, in my service, I handle it like this: 仅供参考,在我的服务中,我会这样处理:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "MyService started");
}
My service never starts. 我的服务永远不会开始。 What's going wrong?
怎么了
For repeating alarm use: 要重复使用闹钟:
// Alarm time :: Current time
Calendar calendar = Calendar.getInstance() ;
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.SECOND, 0);
// Interval
long interval = 3000; // 3 seconds
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);
Instead of: 代替:
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 3000, pendingIntent);
Make sure you have declared MyService
in AndroidManifest.xml
. 确保已在
AndroidManifest.xml
声明了MyService
。
I made a stupid mistake in showing logs. 我在显示日志时犯了一个愚蠢的错误。 So, my code actually works.
因此,我的代码实际上有效。 But, I guess alarmManager.getNextAlarmClock().getTriggerTime() doesn't return in UTC which causing the difference to System.currentTimeMills().
但是,我猜想alarmManager.getNextAlarmClock()。getTriggerTime()不会以UTC返回,这会导致System.currentTimeMills()有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.