我需要检查我的设备是否连接到wifi网络。但是发生的事件让我很困惑。以下是重现的步骤:
所以我注册了一个带有WifiManager.NETWORK_STATE_CHANGED_ACTION)
行动的接收器:
private void registerReceiver() {
Log.v(TAG, "registerReceiver");
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
broadcastReceiver = new ConnectivityBroadcastReceiver();
context.registerReceiver(broadcastReceiver, intentFilter);
}
这就是我收到活动的方式。
private class ConnectivityBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("Broadcast", "onReceive: " + intent.getAction());
final NetworkInfo networkInfo =
intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (networkInfo != null) {
final NetworkInfo.State state = networkInfo.getState();
final NetworkInfo.DetailedState detailedState = networkInfo.getDetailedState();
Log.d(TAG, "NetworkInfo.State: " + state);
Log.d(TAG, "NetworkInfo.DetailedState: " + detailedState);
}
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
final SupplicantState supplicantState = wifiInfo.getSupplicantState();
final NetworkInfo.DetailedState detailedState = wifiInfo.getDetailedStateOf(supplicantState);
Log.d(TAG, "SupplicantState: " + supplicantState.toString());
Log.d(TAG, "DetailedStateOfSupplicant: " + detailedState.toString());
}
}
或
首先,我收到一些关于CONNECTING
和AUTHENTICATING
的事件。这里的一切似乎都很正常。
但是,在一次设置期间,我连续输出四次:
onReceive: Broadcast android.net.wifi.STATE_CHANGE
NetworkInfo.State: CONNECTED
NetworkInfo.DetailedState: CONNECTED
SupplicantState: COMPLETED
DetailedStateOfSupplicant: OBTAINING_IPADDR
根据文档,请求者状态COMPLETED
被映射到OBTAINING_IPADDR
似乎是逻辑:
SupplicantState已完成:
该状态表示请求者已完成其处理 对于关联阶段,数据连接是完全的 配置。但请注意,可能没有任何IP地址 与连接相关联。通常,DHCP请求需要 此时发送以获取地址。
答案 0 :(得分:2)
使用以下类检查网络状态:
public class Connectivity {
public static NetworkInfo getNetworkInfo(Context context){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo();
}
public static boolean isConnected(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected());
}
public static boolean isConnectedWifi(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
public static boolean isConnectedMobile(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}
public static boolean isConnectedFast(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected() && Connectivity.isConnectionFast(info.getType(),info.getSubtype()));
}
public static boolean isConnectionFast(int type, int subType){
if(type== ConnectivityManager.TYPE_WIFI){
return true;
}else if(type==ConnectivityManager.TYPE_MOBILE){
switch(subType){
case TelephonyManager.NETWORK_TYPE_1xRTT:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_CDMA:
return false; // ~ 14-64 kbps
case TelephonyManager.NETWORK_TYPE_EDGE:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return true; // ~ 400-1000 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return true; // ~ 600-1400 kbps
case TelephonyManager.NETWORK_TYPE_GPRS:
return false; // ~ 100 kbps
case TelephonyManager.NETWORK_TYPE_HSDPA:
return true; // ~ 2-14 Mbps
case TelephonyManager.NETWORK_TYPE_HSPA:
return true; // ~ 700-1700 kbps
case TelephonyManager.NETWORK_TYPE_HSUPA:
return true; // ~ 1-23 Mbps
case TelephonyManager.NETWORK_TYPE_UMTS:
return true; // ~ 400-7000 kbps
case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
return true; // ~ 1-2 Mbps
case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
return true; // ~ 5 Mbps
case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
return true; // ~ 10-20 Mbps
case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
return false; // ~25 kbps
case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
return true; // ~ 10+ Mbps
// Unknown
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
default:
return false;
}
}else{
return false;
}
}
}