设备以太网连接状态

问题描述 投票:1回答:1
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打印机,我正在检查以太网连接状态。我总是获得相同的结果。当有线插件拔出时。关于如何跟踪它的任何想法

android connection ethernet status
1个回答
1
投票

要检查以太网连接,请使用:

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();
}
© www.soinside.com 2019 - 2024. All rights reserved.