本文介绍了Android的AlarmManager.set(...):通知从未接受过当电池电量低的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用AlarmManager安排在我的应用程序的延迟检查。 (具体来说,用户接近的位置N分钟以后,我要检查他们是否还在那里,如果是给他们的通知。)

I'm attempting to use AlarmManager to schedule a delayed check in my app. (Specifically, N minutes after a user approaches a location, I want to check whether they're still there, and if so send them a notification.)

我已经通过检查,看他们是否已经进入该地区在我的位置更新接收器和,如果他们有,调度像这样实现的:

I've implemented this by checking to see whether they've entered the region in my location update receiver and, if they have, scheduling like so:

Intent geofenceIntent = new Intent(context, GeofenceReceiver.class)
// ...intent contents not important...
PendingIntent pi = PendingIntent.getBroadcast(context, 0, geofenceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Calendar c = Calendar.getInstance();
c.add(Calendar.SECOND, getGeofenceDelaySeconds());
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
Log.v("Scheduling notification check for "+c.getTime());

当电池电量较高(比如50%),一切就像一个魅力。但是,当它的低(比如10%),我得到的位置更新,他们安排报警貌似成功,但报警从未真正触发!

When the battery level is high (say, 50%), everything works like a charm. But when it's low (say, 10%), I get location updates, they schedule the alarm seemingly-successfully, but that alarm never actually triggers!

怎么办? Android版是否停止发送特定类型的更新时,它试图以节省电力?我该如何解决此问题?

What gives? Does Android stop sending certain types of updates when it's trying to conserve power? How can I work around this (short of actually keeping my app active for the duration of the delay, which causes obvious issues with battery life)?

推荐答案

原来,这是关系到使用的实时时钟。

It turns out that this is related to the use of the real-time clock.

虽然我不能找到它引用的文件(它不是在AlarmManager),表明, AlarmManager.RTC_WAKEUP 如果手机处于节电模式。 AlarmManager.ELAPSED_REALTIME_WAKEUP 警报似乎并没有遭受这个问题,所以我能够通过切换到以解决此问题:

Although I could not find the documentation it quotes (it's not in AlarmManager), this StackOverflow answer suggests that AlarmManager.RTC_WAKEUP alarms do not trigger if the phone is in power-saving mode. AlarmManager.ELAPSED_REALTIME_WAKEUP alarms do not seem to suffer this problem, so I was able to fix the issue by switching to:

Intent geofenceIntent = new Intent(context, GeofenceReceiver.class)
// ...intent contents not important...
PendingIntent pi = PendingIntent.getBroadcast(context, 0, geofenceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

long millis = SystemClock.elapsedRealtime() + 1000 * getGeofenceDelaySeconds();
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, millis, pi);

这篇关于Android的AlarmManager.set(...):通知从未接受过当电池电量低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 21:19