我正在android中编写一个iptv设置框启动器,我正在尝试找出以太网连接信息(用户是否使用以太网电缆连接),请帮助...如果问题不清楚,我深表歉意。请帮助我非常需要它,从过去三天开始就卡住了。
问问题
469 次
2 回答
0
您可以通过以下方式检查您是否连接在以太网或 Wifi 中ConnectivityManager
:
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo ethernet = connManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
// get the ethernet state with : ethernet.getState()
// get the Wifi state with : wifi.getState()
您需要在清单中添加:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
您可以通过 a 注册连接事件BroadcastReceiver
,查看此示例
或者您可以使用为您制作的这个库
于 2016-11-01T12:00:06.477 回答
0
private Boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}
public Boolean isWifiConnected(){
if(isNetworkAvailable()){
ConnectivityManager cm
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return (cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI);
}
return false;
}
public Boolean isEthernetConnected(){
if(isNetworkAvailable()){
ConnectivityManager cm
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return (cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_ETHERNET);
}
return false;
}
然后 :
if(isEthernetConnected()){
// connected
}
else {
//not connected
}
于 2016-11-02T11:18:40.517 回答