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;
|
}
|
}
|
|
}
|
}
|