我有一个应用程序,该应用程序使用 AlarmManager 定期整小时唤醒手机,并向Android Wear手表发送消息,这会引起短暂的震动。我有两个用户在使用带有Android 5.1.1的三星Galaxy S6和5.1.1的Sony SW 3,它们遇到了一个奇怪的错误。在最初的整整一个小时内,振动是在准确的时间进行的,但所有其他振动都延迟了3分钟。有时甚至第一个小时的振动也会被延迟。
I have an app which uses the AlarmManager to regularly wake up the phone at full hour and send a message to an Android Wear watch which than makes a short vibration. I have two users with a Samsung Galaxy S6 with stock Android 5.1.1 and the Sony SW 3 with 5.1.1 who experience a weird bug. At the very first full hour the vibration is at the exact time but all other vibrations are 3 minutes delayed. Sometimes even the first full hour vibration is delayed.
这里是一些代码:
final Calendar time = Calendar.getInstance(); time.set(Calendar.SECOND, 0); time.set(Calendar.MILLISECOND, 0); time.set(Calendar.MINUTE, 0); time.set(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY) + 1); final Intent hourlyChimeIntent = new Intent(context, HourlyChimeReceiver.class); hourlyChimeIntent.setAction(key); final AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); final PendingIntent pi = PendingIntent.getBroadcast(context, 0, hourlyChimeIntent, PendingIntent.FLAG_CANCEL_CURRENT); am.setExact(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pi);我在接收器中获得了 WakeLock 然后在线程中向Wear手表发送消息。不会错过任何振动,它们只迟到了3分钟。
I acquire a WakeLock in the receiver and then send a message to the Wear watch in a thread. No vibration is missed, they are just 3 minutes late.
我没有关于此问题的其他报告,并且我的所有测试设备都运行良好。我没有三星设备。
I have no other reports about this issue and all my testing devices are working good. I have no Samsung device though.
任何想法可能导致3分钟的延迟吗?三星会忽略 setExact 并使我的闹钟不准确吗?如何在Samsung上强制发出确切警报?
Any ideas what could cause the 3 minutes delay? Does Samsung ignore setExact and makes my alarm an inexact? How to force exact alarms on Samsungs?
编辑:
这里是Android Wear专用代码。在接收方的 onReceive 方法中,我这样做:
Here is the Android Wear specific code. In the receiver's onReceive method I do this:
final PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE); final PowerManager.WakeLock lock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID); lock.acquire(7L * 1000L); final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(Wearable.API).build(); new Thread(new Runnable() { @Override public void run() { googleApiClient.blockingConnect(); long pattern[]; pattern = new long[] {0L, 500L}; final NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await(2000L, TimeUnit.MILLISECONDS); if (nodes != null) { for (final Node node : nodes.getNodes()) { // just send and forget Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), "/hourly_chime", Utils.Vibrator.serializeVibratePattern(pattern).getBytes()).await(); } } } }).start();推荐答案
问题似乎仅发生在三星设备上(例如Galaxy Grand,S4,S5,S6,Note 3,Note 4和Lollipop(5.0、5.1、5.1.1)。当设备由电池供电且屏幕关闭时,警报调度的时间似乎不准确。如果设备正在充电或在计划警报期间打开屏幕,则不会发生此问题。
The issue seems to occur only on Samsung devices (e.g. Galaxy Grand, S4, S5, S6, Note 3, Note 4) with Lollipop (5.0, 5.1, 5.1.1). It seems that alarms are scheduled inexact when device is on battery with screen off. If device is charging or has screen on during scheduling alarm the issue will not occur.
您可以通过以下方式验证下一个警报是否不准确:
You can verify if next alarm will be inexact with:
adb shell dumpsys alarm我没有找到解决此问题的完美解决方案-只是每个都有一些缺点的解决方法:
I didn't find perfect solution for this problem - only workarounds where each has some drawbacks:
BTW这个问题使我发疯:)
BTW This issue drives me crazy :)
编辑:当应用包名称中有关键字 alarm或 alert时,看来不会发生此问题(如Mathieu H.在下面的评论中所指出的那样) )。
Looks like this issue does not occur when there is keyword "alarm" or "alert" in the app package name (as pointed out by Mathieu H. in comments below).
我还可以通过在电池设置(或Smart Manager应用)中禁用应用优化来手动解决此问题。似乎无法通过编程方式完成,因此您可以尝试询问用户...
I was also able to fix the issue manually by disabling App optimization in Battery settings (or in Smart Manager app). It seems it cannot be done programmatically, so you can try asking your users...
更多推荐
Android完全闹钟始终关闭3分钟
发布评论