gpt4 book ai didi

java - Android 服务被 android V 2.3 (gingerbread) 杀死

转载 作者:行者123 更新时间:2023-11-30 09:34:04 25 4
gpt4 key购买 nike

我有一个 GPS 服务,它的工作是将坐标获取到服务器中。该服务假定 24/7 全天候运行。但它在两者之间以某种方式被杀死。这仅在 android v 2.3 中发生。在 android v2.2 上它运行良好。

在此服务中,我使用“LocationManager”及其创建循环的方法“requestLocationUpdates”。此循环负责获取坐标。所以我的目标是保持循环运行。

那么该怎么做,让服务 24/7 全天候运行。

最佳答案

This server suppose to be run 24/7

不能那样做。这是 not possible正如您所发现的,在任何真正意义上的术语。也是not a good design choice完全没有。

如果你绝对需要它一直运行,你需要通过 PowerManager 获取 PARTIAL_WAKE_LOCK。这将保留 CPU一直在运行,并且您的程序正在运行。准备一个令人震惊的电池生命周期下降。

改为使用 AlarmManager .您可以安排 PendingIntent通过 AlarmManager 在相关时间点启动服务来完成它的工作。完成后,再次终止该服务。

下面是一个示例代码,显示了如何使用 AlarmManager,它将启动 Intent 以在 5 分钟后启动 YourService:

// get a calendar with the current time
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.MINUTE, 5);

Intent intent = new Intent(ctx, YourService.class);
PendingIntent pi = PendingIntent.getService(this, 123, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);

关于java - Android 服务被 android V 2.3 (gingerbread) 杀死,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11929466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com