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?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Even RTC_WAKEUP doesn't help most of the times.
Solution that worked for my app when device in deep sleep mode:
Use WakefulBroadcastReceiver combined with AlarmManager.
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.
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.
You can use the
AlarmManager
class to wake up the device at a particular time, then fire off an operation at whatever interval you'd like. Code from the docs found here:Notice the last line of this block. You can use the method
setRepeating()
to set whatever interval you'd like.