gpt4 book ai didi

android - AlarmManager RTC_WAKEUP 耗尽电池

转载 作者:行者123 更新时间:2023-11-30 01:21:52 26 4
gpt4 key购买 nike

我有一个在后台运行的服务,它会定期从服务器下载数据。我需要每隔一小时下载一次数据。但是电池消耗得很快。代码如下。如何使闹钟更省电?

public class BackgroundFetchService extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

int s = super.onStartCommand(intent, flags, startId);

BackgroundFetchManager.getSharedInstance().setAlarmStatus(false);

TherapyManager.getSharedInstance().downloadData(new DataManager.DataManagerListener() {
@Override
public void onCompletion(AppError error) {
stopSelf();
}
});

return s;
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onDestroy() {
//restart this service again in one hour

if(!BackgroundFetchManager.getSharedInstance().getAlarmStatus()) {
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm.set(
alarm.RTC_WAKEUP,
System.currentTimeMillis() + (1000 * 60 * 60),
PendingIntent.getService(this, 0, new Intent(this, BackgroundFetchService.class), 0)
);
BackgroundFetchManager.getSharedInstance().setAlarmStatus(true);
}

super.onDestroy();
}
}

最佳答案

尝试仅使用 RTC,因为根据文档:https://developer.android.com/training/scheduling/alarms.html , RTC 在指定时间触发未决 Intent 但不唤醒设备。

例如:

AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm.set(
alarm.RTC,
System.currentTimeMillis() + (1000 * 60 * 60),
PendingIntent.getService(this, 0, new Intent(this, BackgroundFetchService.class), 0)
);
BackgroundFetchManager.getSharedInstance().setAlarmStatus(true);

关于android - AlarmManager RTC_WAKEUP 耗尽电池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36977220/

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