我有一个关于安卓中的ConnectivityManager和NetworkInfo类的问题。
如果我走进蒂姆霍顿、星巴克、McDonalds等所有都有密码或某种代理的免费wifi,布尔isConnected()是否返回正确的标志(在这种情况下,您将连接到wifi,但在登录/注册之前互联网是不可用的)。
如果此标志(或NetworkInfo中的另一个标志)没有返回适当的值,是否有类似于iPhone Reachability类的东西来检查此标志。
谢谢
接入点
发布于 2015-02-11 16:40:13
我使用BroadcastReceiver来接受CONNECTIVITY_CHANGE。
private void debugIntent(Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
for (String key: extras.keySet()) {
LogText.appendLog(TAG + " key [" + key + "]: " +extras.get(key));
}
}
else {
LogText.appendLog(TAG + "no extras");
}
}
@SuppressWarnings("deprecation")
public boolean hasConnectivity(final Context context, final Intent intent) {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
if(currentNetworkInfo != null){
return noConnectivity == false && currentNetworkInfo.isConnected();
}
return false;
}
public class ReachabilityTest extends AsyncTask<Void, Void, Boolean> {
private Context mContext;
private Intent mIntent;
private String mHostname;
private int mServicePort;
public ReachabilityTest(Context context, Intent intent, String hostname, int port) {
mContext = context.getApplicationContext(); // Avoid leaking the Activity!
mIntent = intent;
mHostname = hostname;
mServicePort = port;
}
@Override
protected Boolean doInBackground(Void... args) {
if (hasConnectivity(mContext, mIntent)) {
InetAddress address = isResolvable(mHostname);
if (address != null) {
if (canConnect(address, mServicePort)) {
return true;
}
}
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
Intent newIntent = new Intent(mContext, NetworkConnService.class);
newIntent.putExtra("hasConnectivity", result);
mContext.startService(newIntent);
}
@SuppressWarnings("deprecation")
private boolean hasConnectivity(final Context context, final Intent intent) {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
if(currentNetworkInfo != null){
LogText.appendLog("Current Network Info: " + currentNetworkInfo.getTypeName() + " isConnected: " + currentNetworkInfo.isConnected());
return noConnectivity == false && currentNetworkInfo.isConnected();
}
if(otherNetworkInfo != null)
LogText.appendLog("Other Network Info: " + otherNetworkInfo.getTypeName() + " isConnected: " + otherNetworkInfo.isConnected());
return false;
}
private InetAddress isResolvable(String hostname) {
try {
return InetAddress.getByName(hostname);
}
catch (UnknownHostException e) {
LogText.appendLog("isResolvable " + e.getLocalizedMessage());
return null;
}
}
private boolean canConnect(InetAddress address, int port) {
Socket socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(address, port);
try {
socket.connect(socketAddress, 2000);
}
catch (IOException e) {
return false;
}
finally {
if (socket.isConnected()) {
try {
socket.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
}https://stackoverflow.com/questions/27909083
复制相似问题