暂停服务,直至互联网连接可以建立互联网

由网友(社会不惯人)分享简介:我有运行启动完成后的服务。此服务需要互联网连接。什么是对等的设备连接到互联网的最佳实践? wifi的手机其实并不重要。 我目前的解决方案涉及到只检查ConnectivityManager,直到其中一个网络可用whil​​e循环,但这种感觉低俗。 有没有更好的方式来做到这一点?解决方案 但这种感觉庸俗事实上:D 您的接...

我有运行启动完成后的服务。此服务需要互联网连接。什么是对等的设备连接到互联网的最佳实践? wifi的手机其实并不重要。

我目前的解决方案涉及到只检查ConnectivityManager,直到其中一个网络可用whil​​e循环,但这种感觉低俗。

有没有更好的方式来做到这一点?

解决方案   

但这种感觉庸俗

事实上:D

您的接收器唤醒你清醒的意图服务(可能是一个简单的目的服务会做,因为手机不睡觉,同时启动据我所知) 在服务注册一个接收器连接 在的CountDownLatch服务等待 接收器唤醒服务起来的时候无线连接

骨架code: http://stackoverflow.com/a/19968708/281545 - 你的情况是简单,因为你没有醒了WiFi,持无线门锁等,否则(包括本需要长期和收音机/ CPU睡眠的情况下 - 在这种情况下,一个简单的目的服务,不会做)2和3之间,你需要

2A。服务获得一个的无线锁定的 2B。服务电话重新连接()重新关联()和任何需要的(这可能是特定的设备)

I have a service that runs upon boot completion. This service requires internet connectivity. What's the best practice for waiting for the device to connect to the internet? Mobile of wifi doesn't really matter.

My current solution involves a while loop that just checks ConnectivityManager until one of the networks becomes available, but this feels vulgar.

萌新别方 过来人告诉你,新鲜法律人如何培养实务能力

Is there a better way to do this?

解决方案

but this feels vulgar

Indeed :D

Your receiver wakes your wakeful intent service (probably a simple intent service would do, as the phone does not sleep while booting AFAIK) service registers a receiver for connectivity service waits on a CountDownLatch the receiver wakes the service up when the wifi is connected

Skeleton code : http://stackoverflow.com/a/19968708/281545 - your case is simpler as you do not have to wake the wifi, hold wifi locks etc. Otherwise (including the case this takes long and radios/CPU sleep - in which case a simple intent service won't do) between 2 and 3 you would need to :

2a. service acquires a wifi lock 2b. service calls reconnect(), reassociate() and whatever is needed (this may be device specific)

阅读全文

相关推荐

最新文章