所以实际上你有四种状态(如果互联网存在与否则为第五种)
当网络可用时,您必须通过监控Wifi和移动网络的状态来检查互联网提供商,您的四种状态是
Wifi改变状态和
1.启用移动网络
2.移动网络已禁用
移动网络改变状态和
1.启用Wifi
2. Wifi被禁用
首先,您必须监控互联网是否可用,此后您必须添加类似的两个监听器(广播接收器)以进行Wifi状态更改和移动状态更改并检查所需状态,我已添加状态检查方法下面,
接收实施
Checking for changes in a receiver and then checking state
public class Internet_State extends BroadcastReceiver {
//checked with new state changed when event occurs
public boolean oldInternetState;
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
Bundle b = intent.getExtras();
// if Internet available
boolean isConnected = !b
.getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY);
// ignore if no state change
if (oldInternetState == isConnected)
return;
// set new oldInternetState
oldInternetState = isConnected;
boolean isMobile = activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE;
boolean isWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.isAvailable();
//you can save these states to be monitored in wifi and mobile change change listners
//No internet is 0 state
int state = isConnected?1:0;
if(state)
state = checkState(context)
intent.putExtra("state", state);
// and then send this intent to your required method which will
// check the state and perform function
}
@Override
public void initialize(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
//save the current internet state at start of the receiver registration
oldInternetState = false;
if (activeNetwork != null)
oldInternetState = activeNetwork.isConnectedOrConnecting();
}
}
实现两个,移动和wifi状态更改广播接收器,并更好地添加这样的状态检查方法
检查状态
Monitoring the state out of four states
public int checkState(Context context){
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isMobile = activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE;
boolean isWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.isAvailable();
//No internet is 0 state
int state = isConnected?1:0;
if(isWifi && isMobile)
state = 1;
else if(isWifi && !isMobile)
state = 2;
if(isMobile && isWifi)
state = 3;
else if(isMobile && !isWifi)
state = 4;
return state;
}
并从3 Wifi,Internet和移动数据更改接收方调用此方法,不要忘记检查oldState并将其与更改后的状态匹配.
移动旧州
NetworkInfo allNetInfo = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
oldDataState = allNetInfo.getState();
对于Wifi状态
int state = intent.getExtras().getInt(WifiManager.EXTRA_WIFI_STATE);
if (state == WifiManager.WIFI_STATE_ENABLED)
state = 1;
else if (state == WifiManager.WIFI_STATE_DISABLED)
state = 0;
else
return;
if (state == oldWifiState)
return;
oldWifiState = state;
Problem I get WiFi enabled 2 to 3 times
由于Wifi改变状态,如TurnOn和TurningOf由操作系统触发,因此不止一次调用Wifi发生更改,你必须取消它们,就像我上面所做的那样
if (state == WifiManager.WIFI_STATE_ENABLED)
state = 1;
else if (state == WifiManager.WIFI_STATE_DISABLED)
state = 0;
else
return;
现在只监控启用和禁用.