ソースを参照

添加无法访问服务器的状态到网络判断中

zengjiebin 7 年 前
コミット
4b99e0e8d3
共有2 個のファイルを変更した27 個の追加19 個の削除を含む
  1. 15 17
      app/src/main/java/com/sheep/gamegroup/util/NetUtil.java
  2. 12 2
      app/src/main/java/com/sheep/gamegroup/util/SysAppUtil.java

+ 15 - 17
app/src/main/java/com/sheep/gamegroup/util/NetUtil.java

@@ -15,32 +15,33 @@ import com.sheep.jiuyan.samllsheep.SheepApp;
 public class NetUtil {
     private static NetUtil netUtil;
 
-    public static NetUtil getNetUtil(){
-        if (netUtil == null){
+    public static NetUtil getNetUtil() {
+        if (netUtil == null) {
             netUtil = new NetUtil();
         }
         return netUtil;
     }
+
+    public static boolean CAN_CONNECT_SERVICE = true;//是否可以连接到服务器
+
     /**
      * 判断网络是否连接
      *
      * @param context
      * @return
      */
-    public static boolean isConnected(Context context)
-    {
-
+    public static boolean isConnected(Context context) {
+        if(!CAN_CONNECT_SERVICE){
+            return false;
+        }
         ConnectivityManager connectivity = (ConnectivityManager) SheepApp.getInstance()
                 .getSystemService(Context.CONNECTIVITY_SERVICE);
 
-        if (null != connectivity)
-        {
+        if (null != connectivity) {
 
             NetworkInfo info = connectivity.getActiveNetworkInfo();
-            if (null != info && info.isConnected())
-            {
-                if (info.getState() == NetworkInfo.State.CONNECTED)
-                {
+            if (null != info && info.isConnected()) {
+                if (info.getState() == NetworkInfo.State.CONNECTED) {
                     return true;
                 }
             }
@@ -51,8 +52,7 @@ public class NetUtil {
     /**
      * 判断是否是wifi连接
      */
-    public static boolean isWifi(Context context)
-    {
+    public static boolean isWifi(Context context) {
         ConnectivityManager cm = (ConnectivityManager) SheepApp.getInstance()
                 .getSystemService(Context.CONNECTIVITY_SERVICE);
 
@@ -65,8 +65,7 @@ public class NetUtil {
     /**
      * 判断是否是流量连接
      */
-    public static boolean isMobile(Context context)
-    {
+    public static boolean isMobile(Context context) {
         ConnectivityManager cm = (ConnectivityManager) SheepApp.getInstance()
                 .getSystemService(Context.CONNECTIVITY_SERVICE);
 
@@ -79,8 +78,7 @@ public class NetUtil {
     /**
      * 打开网络设置界面
      */
-    public static void openSetting(Activity activity)
-    {
+    public static void openSetting(Activity activity) {
         Intent intent = new Intent("/");
         ComponentName cm = new ComponentName("com.android.settings",
                 "com.android.settings.WirelessSettings");

+ 12 - 2
app/src/main/java/com/sheep/gamegroup/util/SysAppUtil.java

@@ -275,13 +275,23 @@ public class SysAppUtil {
                 .map(new Func1<Integer, Integer>() {
                     @Override
                     public Integer call(Integer integer) {
+                        int result;
                         try {
                             Response response = OkHttpUtils.get().url("http://www.baidu.com").build().execute();
-                            return response.isSuccessful() ? 0 : -1;
+                            result = response.isSuccessful() ? 0 : -1;
                         } catch (IOException e) {
                             e.printStackTrace();
-                            return -2;
+                            result = -2;
                         }
+                        switch (result){
+                            case 0://有网
+                                NetUtil.CAN_CONNECT_SERVICE = true;
+                                break;
+                            default://无网
+                                NetUtil.CAN_CONNECT_SERVICE = false;
+                                break;
+                        }
+                        return result;
                     }
                 })
                 .subscribeOn(Schedulers.io())