所以实际上你有四个状态为(+第五,如果互联网是有或没有)
当网络可用时,你必须通过监控WiFi和移动网络的状态来检查互联网服务提供商,你的四状态是
无线上网的变化状态和
1.移动网络启用
2.移动网络被禁用
移动网络变化状态和
1.无线上网启用
2。禁用了WiFi,
首先你必须监控如果互联网是否可用,在这之后,你必须添加类似的两个监听器(广播recievers)建立WLAN连接状态变化和移动状态的变化和检查所需的状态下,我已经添加以下状态检查方法,
Reciever实施
检查在接收器中的变化,然后检查状态
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状态变化广播recievers和更好的增加状态检查方法,这样
检查国家
监控状态出的四种状态
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和Mobile调用此方法数据更改接收者,不要忘记检查oldState并将其与状态更改相匹配。建立WLAN连接状态
移动老态的
NetworkInfo allNetInfo = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
oldDataState = allNetInfo.getState();
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;
问题,我得到的支持WiFi 2〜3倍
WIFI的一个以上的电话改变发生,因为Wifi改变状态,如TurningOn和TurningOf由OS触发,你必须消除他们,就像我上面不喜欢
if (state == WifiManager.WIFI_STATE_ENABLED)
state = 1;
else if (state == WifiManager.WIFI_STATE_DISABLED)
state = 0;
else
return;
只有Enabled和Disabled现在被监控。
这就是我的问题。如何用一个触发器注意到这些变化?此外,该技术同时启用移动网络和WiFi,当我们启用它们时。其实,WiFi将会很活跃。当WiFi开启时,我们将不得不动态地决定移动网络已关闭。 – madhu