How to handle WiFi to Mobile network switch programatically?

前端 未结 2 1178
旧时难觅i
旧时难觅i 2020-12-17 04:12

Right now, I am having application which works with WiFi, but while I am going to mobile providers network my application does not working. I have maintained one background

相关标签:
2条回答
  • 2020-12-17 04:41

    I got the solution:

    String networkStatus = "disconnected";
                int netType = 0;
                try{
                ConnectivityManager connectivityManager =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                if(connectivityManager != null ){
                        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    
                        if(networkInfo != null){
                            netType = networkInfo.getType();
                            Log.d("Log", "connetion is available");
                        }else {
                            Log.d("Log", "connetion is  not available");
                            return networkStatus;
                        }
    
                    //  if(networkInfo.isAvailable()){  // Old one
    if(networkInfo.isAvailable() && networkInfo.isConnected()){  // New change added here
                            if(netType == ConnectivityManager.TYPE_WIFI)
                                {}
                            else if(netType == ConnectivityManager.TYPE_MOBILE )
                                {}
                                }
                            }
                        }catch(Exception e){
                Log.d("Log", "checkNetworkConnection" + e.toString());
                return networkStatus;
            }
    
    0 讨论(0)
  • 2020-12-17 04:46

    http://thiranjith.wordpress.com/2011/03/31/how-to-monitor-network-connectivity-in-android/

    Hope this will help you..

    0 讨论(0)
提交回复
热议问题