在服务激活锁定实现 [英] Wakelock implementation in a service

查看:173
本文介绍了在服务激活锁定实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个手机执行的服务。他们相互使用套接字沟通,并在它们之间执行一些脚本(这可能需要1-2分钟左右)和交换文件(使用HTTP POST)。

我想这继续执行,甚至当屏幕处于关闭状态,即获得一个激活锁定。我看到此链接,但我不知道在哪里我的code将这一(如该装置可以在执行的中间入睡)。任何指导是非常AP preciated。谢谢你。


解决方案

  

我为使用相同的应用程序为2个手机。在OnCreate()中启动一个服务端口上监听。在PHONE1点击一个按钮,做了HTTP POST一个文件来PHONE2和保持通信,事情(如手机2还侦听端口最初)。


那么,你的主要目标似乎是引起PHONE2的主人用弯刀来攻击你,因为PHONE2将耗尽的电池。

话虽这么说,你的HTTP后台程序将需要在服务,在这里你获取和释放一个激活锁定(和presumably一个 WifiLock ,因为你想要什么都不会工作在大多数移动数据连接)。

WakefulIntentService 是没有用在这里,因为它是专为明智的场景中,我们需要保持一个激活锁定仅在一段短暂的时间获取的,以完成某些特定的任务。你的情况,你需要保持你的激活锁定无限期收购,因为你不知道什么时候工作可能需要做。


  

我不能确定在哪里我的code合并此。


在服务,presumably。你可能会获得激活锁定的onCreate()的服务,并在的onDestroy释放()的服务。如果你想避免弯刀相关的死亡,你将确保用户有足够的通过此服务正在运行的确切时间控制,因此有足够的控制权,当这个激活锁定生效。

I have a service that executes in 2 phones. They communicate with each other using sockets and execute some script (which may take 1-2 minutes approx.) and exchange files (using HTTP Post) between them.

I want this execution to continue, even when the screen is off, i.e acquire a Wakelock. I saw this link, but I'm not sure where to incorporate this in my code(as the device may sleep in the mid of execution). Any guidance is highly appreciated. Thank you.

解决方案

I m using same app for 2 phones. In onCreate() it starts a service listening on a port. Clicking a button in phone1, does a HTTP Post of a file to phone2 and the communication keeps going on(as phone 2 also listens on a port initially).

Well, your primary objective seems to be to cause the owner of phone2 to attack you with a scimitar, since the phone2 will run out of battery.

That being said, your HTTP daemon will need to be in a service, where you acquire and release a WakeLock (and presumably a WifiLock, since what you want won't work over most mobile data connections).

WakefulIntentService is not useful here, because it is designed for sensible scenarios, where we need to keep a WakeLock acquired only for a brief period of time, to complete some specific task. In your case, you need to keep your WakeLock acquired indefinitely, as you have no idea when work might need to be done.

I m not sure where to incorporate this in my code.

In the service, presumably. You will probably acquire the WakeLock in onCreate() of the service and release it in onDestroy() of the service. If you wish to avoid a scimitar-related demise, you will make sure that the user has plenty of control over exactly when this service is running, and therefore have plenty of control over when this WakeLock is in force.

这篇关于在服务激活锁定实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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