android编程判断是否连接网络的方法wifi_Android编程判断是否连接网络的方法【WiFi及3G判断】

时间:2021-07-01  来源:链接特效  阅读:

判断wifi网络是否链接:

 

 代码如下

publicstaticbooleanisWiFiActive(Context inContext) {

     WifiManager mWifiManager = (WifiManager) inContext

     .getSystemService(Context.WIFI_SERVICE);

     WifiInfo wifiInfo = mWifiManager.getConnectionInfo();

     intipAddress = wifiInfo ==null?0: wifiInfo.getIpAddress();

     if(mWifiManager.isWifiEnabled() && ipAddress !=0) {

     System.out.println("**** WIFI is on");

       returntrue;

     }else{

       System.out.println("**** WIFI is off");

       returnfalse;

     }

}

 

判断3G网络是否链接:

 

 代码如下

publicstaticbooleanisNetworkAvailable( Context context) {

  ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if(connectivity ==null) {

      System.out.println("**** newwork is off");

      returnfalse;

    }else{

      NetworkInfo info = connectivity.getActiveNetworkInfo();

      if(info ==null){

        System.out.println("**** newwork is off");

        returnfalse;

      }else{

        if(info.isAvailable()){

          System.out.println("**** newwork is on");

          returntrue;

        }

      }

    }

     System.out.println("**** newwork is off");

  returnfalse;

}

 

相关权限:

 

 代码如下

 

android编程判断是否连接网络的方法wifi_Android编程判断是否连接网络的方法【WiFi及3G判断】

http://m.bbyears.com/wangyetexiao/127184.html

推荐访问:
相关阅读 猜你喜欢
本类排行 本类最新