在深度睡眠模式下如何使用 CPU 执行任何操作 [英] How to use CPU to perform any operation in Deep Sleep mode

查看:64
本文介绍了在深度睡眠模式下如何使用 CPU 执行任何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 android 的新手.我在大约 3 周内为我的申请而苦恼.我需要在正常模式和睡眠模式下发送和接收数据包.我的应用 必须交换数据 5 秒.我尝试使用 alarmmanager 但在 android 5 上它不起作用.在 android 5 上,间隔更改为 60 秒.这样的解决方案会使电池很快耗尽.当我使用普通异步任务而不是 IntentService 时,它仅在屏幕处于 ONapp 可见时才起作用.当应用程序被隐藏或我点击电源 OFF 然后交换数据停止工作.最好的解决方案是什么?

I'm new in android. I struggle with my application approximately 3 weeks. I need sent and receive packets in normal mode and sleep mode. My app must exchange data a 5 seconds. I tried using alarmmanager but on android 5 it's not works. On android 5 an interval changes it on 60 seconds. Such a solution makes the battery wears out quickly. When I use normal asynctask, not IntentService, then it works only when screen is ON and app is visible. When app is hidden or I click power OFF then exchange data stops working. What is the best solutions?

推荐答案

即使 RTC_WAKEUP 在大多数情况下也无济于事.

Even RTC_WAKEUP doesn't help most of the times.

当设备处于深度睡眠模式时适用于我的应用的解决方案:
WakefulBroadcastReceiver 与 AlarmManager 结合使用.

Solution that worked for my app when device in deep sleep mode:
Use WakefulBroadcastReceiver combined with AlarmManager.

Service 由 startWakefulService() 启动,当它完成时,它通过调用 completeWakefulIntent(intent) 来释放唤醒锁.因此,设备将被允许重新进入睡眠状态.

Service is started by startWakefulService() and when it is finished, it releases the wake lock by calling completeWakefulIntent(intent). So the device will be allowed to go back to sleep.

我没有添加任何代码.搜索有关如何将 WakefulBroadcastReceiver 与 AlarmManager 结合使用的示例.甚至 WakefulBroadcastReceiver 文档也有一些模板代码.

I'm not adding any code. Search for examples on how to use WakefulBroadcastReceiver with AlarmManager. Even WakefulBroadcastReceiver doc has some template code.

同时减少闹钟的频率,这样您就可以避免消耗过多的电量.

Also reduce the frequency of alarm so you can avoid draining so much battery.

这篇关于在深度睡眠模式下如何使用 CPU 执行任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆