xuxiuxi
2017-07-28 789b7ff1e420aff8bca618ea1705eb20f4ad0315
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package cn.com.basic.face.service;
 
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
 
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
 
import cn.com.basic.face.base.BaseApplication;
import cn.com.basic.face.base.MainActivity;
import cn.com.basic.face.discern.common.CommonVariables;
import cn.com.basic.face.util.AppApi;
 
public class InternetAccessThread extends Thread {
    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(2* 1000);
                boolean hasInternetAccess = InternetAccess.getNetWorkInfo();
                if (hasInternetAccess) {
                    MainActivity.getInstance().setInternetAccessible(CommonVariables.InternetAccess.INTERNET_ACCESSIBLE);
                } else {
                    MainActivity.getInstance().setInternetAccessible(CommonVariables.InternetAccess.INTERNET_NOT_ACCESSIBLE);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    public static class InternetAccess {
 
        public static boolean getNetWorkInfo() {
            try {
                ConnectivityManager manager = (ConnectivityManager) MainActivity.getInstance()
                        .getApplicationContext().getSystemService(
                                Context.CONNECTIVITY_SERVICE);
                if (manager == null) {
                    return false;
                }
                NetworkInfo networkinfo = manager.getActiveNetworkInfo();
                if (networkinfo == null || !networkinfo.isAvailable()
                        || !networkinfo.isConnectedOrConnecting()) {
                    return false;
                } else {
                    if (openUrl()) {
                        return true;
                    } else {
                        return false;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
 
 
        public static boolean openUrl() {
            StringBuilder total = new StringBuilder();
            try {
                URL url = new URL(AppApi.BASE_URL);
                URLConnection urlCon = url.openConnection();
                urlCon.setConnectTimeout(1500);
                InputStream is = urlCon.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
 
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(bis));
 
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    total.append(line).append('\n');
                }
                bufferedReader.close();
                bis.close();
                is.close();
            } catch (Exception e) {
                //e.printStackTrace();
                System.out.println(e.getMessage());
                return false;
            }
 
            if (total.toString().indexOf("Hello World!") > -1) {
                return true;
            } else {
                return false;
            }
        }
 
    }
}