首页 文章

检测WiFi或移动网络

提问于
浏览
5

我需要注意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 回答

  • 1

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

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

    Wifi改变状态和

    1. Mobile network is enabled 2. Mobile network is disabled

    移动网络改变状态和

    1. Wifi is enabled 2. Wifi is disabled

    首先,您必须监控互联网是否可用,此后您必须添加类似的两个监听器(广播接收器)以进行Wifi状态更改和移动状态更改并检查所需状态,我已添加状态检查方法下面,

    Reciever Implementation

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

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

    Check State

    监控四个州的州

    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改变状态,如TurnOn和TurningOf由操作系统触发,因此不止一次调用Wifi发生更改,你必须取消它们,就像我上面所做的那样

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

    现在只监控启用和禁用 .

  • 0
    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" />
    
  • 0

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

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

  • 1

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

    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触发 .

相关问题