简体   繁体   English

具有PARTIAL_WAKE_LOCK的Android后台服务

[英]Android Background Service with PARTIAL_WAKE_LOCK

So... 所以...

I have a service that spawns a thread that is listening to data from a device via the device's wifi network. 我有一项服务,该服务会产生一个线程,该线程正在通过设备的wifi网络监听来自设备的数据。 All works well, except that the service seems to pause when the the device is put to sleep. 一切正常,除了设备进入睡眠状态时服务似乎暂停。

I've tried PARTIAL_WAKE_LOCK and WIFI_LOCK with no success. 我尝试了PARTIAL_WAKE_LOCK和WIFI_LOCK,但没有成功。 I've checked and it says the wake lock is held, but as soon as the device goes to sleep the service stops logging the data. 我已经检查过了,它说唤醒锁被保持了,但是一旦设备进入睡眠状态,服务就会停止记录数据。 Any thoughts or examples would be greatly appreciated! 任何想法或例子将不胜感激!

Thanks! 谢谢!

Try running the service as Foreground using notification . 尝试使用notification作为Foreground运行服务。 This will make sure your service is always alive. 这样可以确保您的service始终有效。 When your are done with the job, you can Self Stop the service. 完成工作后,您可以Self Stop服务。

Create a notification, showing that your service is running 创建一个通知,显示您的服务正在运行

 startForeground(NOTIFICATION_ID, notification);

So I figured it out. 所以我想通了。 I was improperly calling the WIFI_LOCK. 我不正确地拨打了WIFI_LOCK。 The service was indeed still running, but the wifi connection was going to sleep (I guess). 该服务确实仍在运行,但是wifi连接即将进入睡眠状态(我想)。 After backing up and starting again, I realized that the WIFI_LOCK was not getting created appropriately. 备份并重新启动后,我意识到未正确创建WIFI_LOCK。 Thanks to Libin for sharing the startForeground answer as well. 感谢Libin也分享了startForeground答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM