ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
导致日志
android.net.conn.CONNECTIVITY_CHANGE
getConnectivityStatusString = TYPE_ETHERNET
activeNetwork.getTypeName()= ETHERNET
activeNetwork.isConnected()=真
activeNetwork.isConnectedOrConnecting()=真
activeNetwork.isAvailable()=真
activeNetwork.getState()= CONNECTED
我正在使用Android打印机,我正在检查以太网连接状态。我总是获得相同的结果。当有线插件拔出时。关于如何跟踪它的任何想法
要检查以太网连接,请使用:
public Boolean isEthernetConnected(){
if(isNetworkAvailable()){
ConnectivityManager cm
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return (cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_ETHERNET);
}
return false;
}
这是isNetworkAvailable()
private Boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}