假设设备上同时启用了WIFI和Data / 3G,我如何检查用户当前是否通过wifi或数据计划使用互联网,假设它们都已启用。所以我不需要检查它们是启用还是禁用,我想检测用户实际使用的是哪一个。
我一直在做以下,这是唯一的解决方案吗?
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()) == NetworkInfo.DetailedState.CONNECTED) {
String ssid = wifiInfo.getSSID();
}
答案 0 :(得分:26)
void chkStatus() {
final ConnectivityManager connMgr = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnectedOrConnecting ()) {
Toast.makeText(this, "Wifi", Toast.LENGTH_LONG).show();
} else if (mobile.isConnectedOrConnecting ()) {
Toast.makeText(this, "Mobile 3G ", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "No Network ", Toast.LENGTH_LONG).show();
}
}
答案 1 :(得分:6)
您可以使用以下方法每次检查网络状态:
public static String checkNetworkStatus(final Context context) {
String networkStatus = "";
// Get connect mangaer
final ConnectivityManager connMgr = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
// check for wifi
final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
// check for mobile data
final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if( wifi.isAvailable() ) {
networkStatus = "wifi";
} else if( mobile.isAvailable() ) {
networkStatus = "mobileData";
} else {
networkStatus = "noNetwork";
}
return networkStatus;
} // end checkNetworkStatus
// vKj
答案 2 :(得分:2)
getNetworkInfo
是一种弃用的方法。
我建议使用getActiveNetworkInfo
示例
private String checkNetworkStatus(Context context) {
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;
}
答案 3 :(得分:0)
sudo('echo "g\r\
n\r\
1\r\
\r\
\r\
w\r\
"|fdisk /dev/xvdb', pty=False)
sudo("mkfs.ext4 /dev/xvdb", pty=False)
sudo("mkdir -p /mnt/couchbase")
append('/etc/fstab',"/dev/xvdb1 /mnt/couchbase ext4 defaults 0 0", use_sudo=True)
sudo('mount /mnt/couchbase')