询问有关AlarmManager和WakeLocks

编程入门 行业动态 更新时间:2024-10-08 23:02:25
本文介绍了询问有关AlarmManager和WakeLocks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我开发运行备份操作每隔30分钟本地Android应用程序。

I am developing a native android app that run a backup operation every 30 mins.

我使用 AlarmManager 为此,它工作正常。这里是code我使用的启动报警:

I am using AlarmManager for this purpose and it works fine. Here is the code I am using to start the alarm:

public static void startSync(Context context) { alarmIntent = new Intent(context, AlarmReceiver.class); pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0); manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); // int interval = 3600000; int interval =30000 ; manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent); ComponentName receiver = new ComponentName(context, SampleBootReceiver.class); PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); Toast.makeText(context, "Sync Started", Toast.LENGTH_SHORT).show(); }

这里是上接收方式:

And here is the the on receive method:

public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent arg1) { PowerManager pm = (PowerManager) context.getSystemService(context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag"); wl.acquire(); Intent eventService = new Intent(context, SyncInBackground.class); context.startService(eventService); wl.release(); } }

我注意到,当我的设备不处于待机状态,操作花了5秒(以编程方式计算的话),但是当移动处于待机模式下,它用了11秒。这就是为什么我用 WAKE_LOCK 运行在后台服务的备份操作,以使应用程序之前只需5秒。

I noticed that when my device isn't in standby, the operation took 5 seconds (I calculated it programmatically) but when the mobile is in standby mode it took 11 seconds. That's why I used wake_lock before running the backup operation in a background service, in order to make the app takes only 5 seconds.

但我仍然得到同样的结果,如果在待机模式下移动......它仍然需要11秒5秒,如果不是处于待机模式。

But I still get the same results if the mobile in standby mode... it still takes 11 seconds and 5 seconds if not in standby mode.

我能做些什么让我的后台服务运行在5秒钟内而不是11秒的重复报警?

What can I do to make my background service run the repeating alarm in 5 seconds instead of 11 seconds?

推荐答案

通常的错误:收购的的onReceive的唤醒锁什么都不做。该AlarmManager已持有的onReceive唤醒锁。的你的工作方式出纯粹是运气,当/如果它工作的。你必须要么使用一个WakefulBroadcastReceiver或使用 WakefulIntentService 。该WIS将获得一个静态唤醒锁,这将是积极的onReceive回归与服务之间启动。

The usual mistake: acquiring a wake lock in the OnReceive does nothing. The AlarmManager already holds a wake lock in OnReceive. Your way works out of pure luck, when/if it works. You have to either use a WakefulBroadcastReceiver or use a WakefulIntentService. The WIS will acquire a static wake lock which will be active between OnReceive returning and the service starting.

在这里看到我的回答:Wake锁工作不正常的链接。

See my answer here: Wake Lock not working properly for links.

更多推荐

询问有关AlarmManager和WakeLocks

本文发布于:2023-11-27 11:06:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637816.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AlarmManager   WakeLocks

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!