暂停服务,直至互联网连接可以建立 [英] Pausing service until internet connection can be established

查看:156
本文介绍了暂停服务,直至互联网连接可以建立的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

解决方案
  

但这种感觉庸俗

事实上:D

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

骨架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

  1. Your receiver wakes your wakeful intent service (probably a simple intent service would do, as the phone does not sleep while booting AFAIK)
  2. service registers a receiver for connectivity
  3. service waits on a CountDownLatch
  4. 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)

这篇关于暂停服务,直至互联网连接可以建立的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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