询问有关AlarmManager和WakeLocks [英] Inquiry related to AlarmManager and 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或使用<一个href=\"https://www.google.be/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCEQFjAA&url=https%3A%2F%2Fgithub.com%2Fcommonsguy%2Fcwac-wakeful&ei=_JfXVJ6EN6Pe7Abxn4C4DA&usg=AFQjCNG72hXtdajjsqnYixgBdYDNmOxhlw&bvm=bv.85464276,d.ZGU\"相对=nofollow> 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.
在这里看到我的回答:<一href=\"http://stackoverflow.com/questions/27903534/wake-lock-not-working-properly/27953337#27953337\">Wake锁工作不正常的链接。
See my answer here: Wake Lock not working properly for links.
这篇关于询问有关AlarmManager和WakeLocks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!