开发者

Android service sleeping after a prolonged period of time

I'm writing a piece of software for android OS that connects to a server and should stay connected until the program is closed. The connection is running in the background as a service and as far as i've gathered services in android should stay connected indefinitely.

However after several hours (timeframe appears random) the service disconnects for no apparent reason but doesn't seem to fire any of the disconnection events. The server regularly pings the connected device which I would have 开发者_如何转开发thought would keep the service alive.

Ive tried using WakeLocks which dont seem to make any difference and also startForeground() which also doesnt seem to work. I'm attempting to use the AlarmManager to wake up the application after a period of time but this feels like a bit of a work around. Has anyone got any suggestions? or is there anything obvious ive missed?

thanks for your help in advance.


as far as i've gathered services in android should stay connected indefinitely.

That is incorrect in general.

However after several hours (timeframe appears random) the service disconnects for no apparent reason but doesn't seem to fire any of the disconnection events.

LogCat will probably indicate that your process has been terminated.

Ive tried using WakeLocks which dont seem to make any difference

Since the phone will fall asleep without one, a WakeLock would be essential for your app, at least if you are on WiFi. Inbound packets on an open 3G socket should automatically wake up the phone, as I understand it, but I haven't tried it.

However, the phone should fall asleep much quicker than "several hours", so I am assuming you are keeping the device awake by other means (e.g., plugged into USB).

and also startForeground() which also doesnt seem to work

Now that should have been sufficient to keep you from being killed, AFAIK. The trade-off is the Notification, of course.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜