问候!!
我遇到了一个问题,我使用了一个连接管理器类来获取网络状态。该类在90%的Android设备上工作正常,但它在某些设备中无效。
查看我的代码。发布在下面 -
public class Connectivity {
public static boolean isConnected(Context context){
boolean found=false;
ConnectivityManager manager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network=manager.getActiveNetworkInfo();
if (network!=null && network.isAvailable() && network.isConnected())
found=true;
return found;
}
}
答案 0 :(得分:0)
public static boolean isConnectingToInternet(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
答案 1 :(得分:0)
public static boolean isDeviceConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
您的代码中的一个小改动肯定会帮助您......