检测WiFi或移动网络

时间:2014-01-30 11:44:46

标签: android

我需要注意android系统中的网络变化并相应地执行特定的活动。

1)启用WiFi时,执行功能A. 2)启用移动网络时,执行功能B. 3)当禁用WiFi并启用移动网络时,停止功能A并启动功能B. 4)当禁用移动网络并启用WiFi时,停止功能B并启动功能A. 5)当没有网络可用时,不执行任何功能。

我尝试使用CONNECTIVITY_CHANGE。有了这个,我多次得到意图,我也知道它的原因。还有另一个问题。考虑移动网络是开启的。现在,当我启用WiFi时,我启用WiFi 2至3次,同时,移动网络禁用将无法确认。

我也尝试使用PhoneStateListener.LISTEN_DATA_CONNECTION_STATE进行注册。它几乎可以工作,但是,当WiFi和移动网络都打开时,我知道WiFi将处于活动状态,当我关闭WiFi时,有时我会收到支持移动网络的消息,有时我不会。

我需要解决这个问题。请帮助我提供指导。

4 个答案:

答案 0 :(得分:1)

    Please find the below code. I suppose it is what you want. In case I misunderstood you, then please let me know:


    public void checkWifiMobileNetwork(Context context){

            ConnectivityManager conMgr =  (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();

            if (activeNetwork != null && activeNetwork.isConnected()) {
                android.net.NetworkInfo connWifiType = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                android.net.NetworkInfo connMobileType = conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

                if(connWifiType.isConnectedOrConnecting()){
                    Log.d("NETWORK_STATUS","WIFI_IS_THERE");
                }else{
                    Log.d("NETWORK_STATUS","WIFI_IS_NOT_THERE");
                }

                if(connMobileType.isConnectedOrConnecting()){
                    Log.d("NETWORK_STATUS","MOBILE_NETWORK_IS_THERE");
                }else{
                    Log.d("NETWORK_STATUS","MOBILE_NETWORK_IS_NOT_THERE");
                }

            }else {
                //notify user you are not online
                Log.d("NETWORK_STATUS","WIFI_MOBILE_IS_NOT_THERE");

            }
        }

Set permission at Manifest:
    <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" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

答案 1 :(得分:1)

所以实际上你有四种状态(如果互联网存在与否,则为+)

当网络可用时,您必须通过监控Wifi和移动网络的状态来检查互联网服务提供商,您的四种状态是

Wifi更改状态和

<强> 1。移动网络已启用
 2.移动网络已禁用

移动网络更改状态和

<强> 1。启用Wifi
 2.禁用Wifi

首先你必须监控互联网是否可用,之后你必须添加类似的两个监听器(广播接收器),用于Wifi状态改变和移动状态改变并检查所需的状态,我已添加状态检查方法如下,

接收方实施

  

检查接收器中的更改然后检查状态

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状态更改广播接收器,并更好地添加像这样的状态检查方法

检查状态

  

监控四个州的状态

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;
  

问题我启用WiFi 2至3次

Wifi更改发生多次调用,因为Wifi更改状态,如TurnOn和TurningOf由操作系统触发,您必须取消它们,就像我上面所做的那样

if (state == WifiManager.WIFI_STATE_ENABLED)
        state = 1;
    else if (state == WifiManager.WIFI_STATE_DISABLED)
        state = 0;
    else
        return;

现在只监控启用和禁用。

答案 2 :(得分:0)

您可以尝试注册广播接收器以注意网络更改。请参阅:http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

小心,状态可以改变太多次,每次你的广播都会被触发。

答案 3 :(得分:0)

在应用程序的第一个活动中尝试此代码。

public class YourActivity extends Activity {

    Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        this.registerReceiver(this.mNetworkConReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); // Here you register your receiver
    }

    private BroadcastReceiver mNetworkConReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            ConnectivityManager conMngr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo wifi = conMngr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            NetworkInfo mobile = conMngr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

            if(wifi.isConnected()){
                // Wifi is connected
            }else if(mobile.isConnected() && wifi.isConnected()){
                // Mobile Network and Wifi is connected
            }// Others conditions here...

        }
    };

}

请记住:每当网络状态发生变化时,您的广播接收器都会被Android触发。