我想知道我的手机使用的互联网连接类型(蜂窝或无线网络(。
Network network;
manager = (ConnectivityManager) this.getSystemService(this.CONNECTIVITY_SERVICE);
manager.getActiveNetworkInfo();
if (manager != null){
if (manager.getNetworkCapabilities(network).hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)){
Toast.makeText(this, "Mobile Internet", Toast.LENGTH_SHORT).show();
}
}
错误消息显示:
(尝试在空对象引用上调用虚拟方法'booleanandroid.net.NetworkCapabilities.hasTransport(int('(。
试试这个。
String networkStatus ="";
final ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
//Check Wifi
final android.net.NetworkInfo wifi = manager.getActiveNetworkInfo();
//Check for mobile data
final android.net.NetworkInfo mobile = manager.getActiveNetworkInfo();
if( wifi.getType() == ConnectivityManager.TYPE_WIFI) {
networkStatus = "wifi";
}else if(mobile.getType() == ConnectivityManager.TYPE_MOBILE){
networkStatus = "mobileData";
}else{
networkStatus="noNetwork";
}
return networkStatus;
您可以使用以下代码进行检查:
private void checkInternetConnectionType() {
boolean isConnected = false;
NetworkInfo activeNetwork = null;
ConnectivityManager cm =
(ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
activeNetwork = cm.getActiveNetworkInfo();
isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
}
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
boolean isMobileData = activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE;
}
向安卓清单添加以下权限.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
另外,请查看以下文档: https://developer.android.com/training/monitoring-device-state/connectivity-monitoring#java
https://developer.android.com/reference/android/net/ConnectivityManager.html
使用波纹管类的 getCurrentNetworkType 函数(您可以修改类并且只有 1 个函数,这个类是我项目的一部分(:
public class ConnectionManagerK {
private static String TAG = "ConnectionManagerK";
private static ConnectionManagerK Instance;
public static ConnectionManagerK getInstance() {
ConnectionManagerK localInstance = Instance;
if (localInstance == null) {
synchronized (ConnectionManagerK .class) {
localInstance = Instance;
if (localInstance == null) {
Instance = localInstance = new ConnectionManagerK ();
}
}
}
return localInstance;
}
public static int getCurrentNetworkType() {
if (isConnectedOrConnectingToWiFi()) {
return StatsController.TYPE_WIFI;
} else if (isRoaming()) {
return StatsController.TYPE_ROAMING;
} else {
return StatsController.TYPE_MOBILE;
}
}
public static boolean isNetworkOnline() {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
if (netInfo != null && (netInfo.isConnectedOrConnecting() || netInfo.isAvailable())) {
return true;
}
netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else {
netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
}
} catch (Exception e) {
FileLog.e(e);
return true;
}
return false;
}
public static boolean isRoaming() {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
if (netInfo != null) {
return netInfo.isRoaming();
}
} catch (Exception e) {
FileLog.e(e);
}
return false;
}
public static boolean isConnectedOrConnectingToWiFi() {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo.State state = netInfo.getState();
if (netInfo != null && (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING || state == NetworkInfo.State.SUSPENDED)) {
return true;
}
} catch (Exception e) {
FileLog.e(e);
}
return false;
}
public static boolean isConnectedToWiFi() {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
} catch (Exception e) {
FileLog.e(e);
}
return false;
}}