首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AlarmManager RTC_WAKEUP排水电池

AlarmManager RTC_WAKEUP排水电池
EN

Stack Overflow用户
提问于 2016-05-02 07:31:48
回答 1查看 806关注 0票数 1

我有一个运行在后台的服务,它定期从服务器下载数据。我需要每隔一小时下载一次数据。但电池消耗得很快。代码如下。如何提高报警电源的效率?

代码语言:javascript
复制
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();
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-05-02 08:30:23

尝试只使用RTC,因为根据文档:https://developer.android.com/training/scheduling/alarms.htmlRTC在指定的时间触发挂起的意图,但不会唤醒设备。

例如:

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36977220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档