package com.streambus.setting;
import java.io.File;
import java.io.IOException;
import android.annotation.SuppressLint;
import android.graphics.drawable.AnimationDrawable;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.streambus.setting.util.Constant;
import com.streambus.setting.util.Util;
import com.streambus.setting.util.WifiUtil;
public class NetTestActivity extends BaseActivity {
private LinearLayout ll_localNet,ll_internet,ll_dns,ll_source;
private TextView tv_localNet,tv_internet,tv_dns,tv_source,tv_result;
private ImageView iv_test2,iv_test3;
private Thread t;
private ImageView test1,test2;
private AnimationDrawable ad1,ad2;
private boolean isLocal,isPing,isDns,isExtra;
@SuppressLint("HandlerLeak")
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case Constant.Hanler.NET_PING_FAIL://外网连接失败
tv_internet.setText("Outside the network disconnected");
isPing=false;
ad2.stop();
test2.setBackgroundColor(getResources().getColor(R.color.gray_line));
break;
case Constant.Hanler.NET_PING_SUCCEEDED://外网连接正常
tv_internet.setText("Outside the network connection is normal");
iv_test3.setBackgroundResource(R.drawable.net_test_3);
isPing=true;
ad2.stop();
test2.setBackgroundColor(getResources().getColor(R.color.blue));
break;
case Constant.Hanler.NET_LOCAL_FAIL://本地网络连接异常
isLocal=false;
tv_localNet.setText("local network disconnected");
ad1.stop();
test1.setBackgroundColor(getResources().getColor(R.color.gray_line));
break;
case Constant.Hanler.NET_LOCAL_SUCCEEDED://本地网络连接正常
isLocal=true;
tv_localNet.setText("local network connected");
iv_test2.setBackgroundResource(R.drawable.net_test_2);
ad1.stop();
test1.setBackgroundColor(getResources().getColor(R.color.blue));
break;
case Constant.Hanler.NET_DNS_FAIL://DNS配置异常
isDns=false;
tv_dns.setText("The DNS configuration errors");
break;
case Constant.Hanler.NET_DNS_SUCCEEDED://DNS配置正确
isDns=true;
tv_dns.setText("The DNS configuration correctly");
break;
case Constant.Hanler.EXTERNALSTORAGE_SUCCEEDED:
isExtra=true;
tv_source.setText("Stored properly set");
break;
case Constant.Hanler.EXTERNALSTORAGE_FAILED:
isExtra=false;
tv_source.setText("Stored properly failed");
break;
case Constant.Hanler.NET_LOCAL_TESTING://本地网络正在测试
ll_localNet.setVisibility(View.VISIBLE);
tv_localNet.setText("Are testing the local network");
ad1.start();
ad2.stop();
break;
case Constant.Hanler.NET_PING://外网正在测试
ll_internet.setVisibility(View.VISIBLE);
tv_internet.setText("Are testing the outside network");
ad2.start();
break;
case Constant.Hanler.NET_DNS_TESTING://DNS正在测试
ll_dns.setVisibility(View.VISIBLE);
tv_dns.setText("Is testing the DNS configuration");
break;
case Constant.Hanler.EXTERNALSTORAGE_TESTING://
ll_source.setVisibility(View.VISIBLE);
tv_source.setText("Is testing of common resources");
break;
case Constant.Hanler.NET_TEST_RESULT:
if(isDns && isLocal && isPing){
tv_result.setVisibility(View.VISIBLE);
}
break;
default:
break;
}
};
};
@Override
public void setContentView() {
setContentView(R.layout.activity_net_test);
}
@Override
public void findView() {
ll_dns=(LinearLayout) findViewById(R.id.ll_dns);
ll_internet=(LinearLayout) findViewById(R.id.ll_internet);
ll_localNet=(LinearLayout) findViewById(R.id.ll_local_net);
ll_source=(LinearLayout) findViewById(R.id.ll_source);
tv_dns=(TextView) findViewById(R.id.tv_dns);
tv_internet=(TextView) findViewById(R.id.tv_internet);
tv_localNet=(TextView) findViewById(R.id.tv_local_net);
tv_source=(TextView) findViewById(R.id.tv_source);
tv_result=(TextView) findViewById(R.id.tv_result);
iv_test2=(ImageView) findViewById(R.id.iv_test_2);
iv_test3=(ImageView) findViewById(R.id.iv_test_3);
test1=(ImageView) findViewById(R.id.test1);
test2=(ImageView) findViewById(R.id.test2);
}
@Override
public void initData() {
setTitle("Network Test");
ad1=(AnimationDrawable) test1.getBackground();
ad2=(AnimationDrawable) test2.getBackground();
t=new Thread(new Runnable() {
@Override
public void run() {
//1.判断本地网络是否连接
handler.sendEmptyMessage(Constant.Hanler.NET_LOCAL_TESTING);
try {
t.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(Util.isInternetEnabled(NetTestActivity.this)){
handler.sendEmptyMessage(Constant.Hanler.NET_LOCAL_SUCCEEDED);
//2.开始判断外网是否连接
handler.sendEmptyMessage(Constant.Hanler.NET_PING);
try {
t.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String ip = "www.baidu.com";// ping 的地址,可以换成任何一种可靠的外网
String ip2="www.google.cn";
boolean ping1=ping(ip);
boolean ping2=ping(ip2);
if(ping1 || ping2){
handler.sendEmptyMessage(Constant.Hanler.NET_PING_SUCCEEDED);
}else{
handler.sendEmptyMessage(Constant.Hanler.NET_PING_FAIL);
}
//检查DNS设置
handler.sendEmptyMessage(Constant.Hanler.NET_DNS_TESTING);
try {
t.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
checkDNS();
//如果是wifi连接,显示wifi连接信号
handler.sendEmptyMessage(Constant.Hanler.EXTERNALSTORAGE_TESTING);
try {
t.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
testSDcard();
handler.sendEmptyMessage(Constant.Hanler.NET_TEST_RESULT);
}else{
handler.sendEmptyMessage(Constant.Hanler.NET_LOCAL_FAIL);
}
}
});
t.start();
}
@Override
public void setEventListener() {
}
private void checkDNS(){
if(WifiUtil.isEthConnected(this)){
String dns=Util.getEthernetMacAddress();
if(Util.isNullStr(dns)){
handler.sendEmptyMessage(Constant.Hanler.NET_DNS_FAIL);
}else{
handler.sendEmptyMessage(Constant.Hanler.NET_DNS_SUCCEEDED);
}
}else if(WifiUtil.isWifiConnected(this)){
String dns=Util.getWifiMacAddress(this);
if(Util.isNullStr(dns)){
handler.sendEmptyMessage(Constant.Hanler.NET_DNS_FAIL);
}else{
handler.sendEmptyMessage(Constant.Hanler.NET_DNS_SUCCEEDED);
}
}
}
private void testSDcard(){
if(Util.isSdcardMounted()){
if(Environment.getExternalStorageDirectory().canWrite()){
String path=Environment.getExternalStorageDirectory().getPath();
File f=new File(path+"/test");
try {
f.createNewFile();
f.delete();
handler.sendEmptyMessage(Constant.Hanler.EXTERNALSTORAGE_SUCCEEDED);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
handler.sendEmptyMessage(Constant.Hanler.EXTERNALSTORAGE_FAILED);
}
}else{
handler.sendEmptyMessage(Constant.Hanler.EXTERNALSTORAGE_FAILED);
}
}
private boolean ping(String ip) {
String result = null;
try {
Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);// ping网址3次
// 读取ping的内容,可以不加
/* InputStream input = p.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuffer stringBuffer = new StringBuffer();
String content = "";
while ((content = in.readLine()) != null) {
stringBuffer.append(content);
} */
// ping的状态
int status = p.waitFor();
if (status == 0) {
result = "success";
return true;
} else {
result = "failed";
}
} catch (IOException e) {
result = "IOException";
} catch (InterruptedException e) {
result = "InterruptedException";
} finally {
Log.d("----result---", "result = " + result);
}
return false;
}
}
import java.io.File;
import java.io.IOException;
import android.annotation.SuppressLint;
import android.graphics.drawable.AnimationDrawable;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.streambus.setting.util.Constant;
import com.streambus.setting.util.Util;
import com.streambus.setting.util.WifiUtil;
public class NetTestActivity extends BaseActivity {
private LinearLayout ll_localNet,ll_internet,ll_dns,ll_source;
private TextView tv_localNet,tv_internet,tv_dns,tv_source,tv_result;
private ImageView iv_test2,iv_test3;
private Thread t;
private ImageView test1,test2;
private AnimationDrawable ad1,ad2;
private boolean isLocal,isPing,isDns,isExtra;
@SuppressLint("HandlerLeak")
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case Constant.Hanler.NET_PING_FAIL://外网连接失败
tv_internet.setText("Outside the network disconnected");
isPing=false;
ad2.stop();
test2.setBackgroundColor(getResources().getColor(R.color.gray_line));
break;
case Constant.Hanler.NET_PING_SUCCEEDED://外网连接正常
tv_internet.setText("Outside the network connection is normal");
iv_test3.setBackgroundResource(R.drawable.net_test_3);
isPing=true;
ad2.stop();
test2.setBackgroundColor(getResources().getColor(R.color.blue));
break;
case Constant.Hanler.NET_LOCAL_FAIL://本地网络连接异常
isLocal=false;
tv_localNet.setText("local network disconnected");
ad1.stop();
test1.setBackgroundColor(getResources().getColor(R.color.gray_line));
break;
case Constant.Hanler.NET_LOCAL_SUCCEEDED://本地网络连接正常
isLocal=true;
tv_localNet.setText("local network connected");
iv_test2.setBackgroundResource(R.drawable.net_test_2);
ad1.stop();
test1.setBackgroundColor(getResources().getColor(R.color.blue));
break;
case Constant.Hanler.NET_DNS_FAIL://DNS配置异常
isDns=false;
tv_dns.setText("The DNS configuration errors");
break;
case Constant.Hanler.NET_DNS_SUCCEEDED://DNS配置正确
isDns=true;
tv_dns.setText("The DNS configuration correctly");
break;
case Constant.Hanler.EXTERNALSTORAGE_SUCCEEDED:
isExtra=true;
tv_source.setText("Stored properly set");
break;
case Constant.Hanler.EXTERNALSTORAGE_FAILED:
isExtra=false;
tv_source.setText("Stored properly failed");
break;
case Constant.Hanler.NET_LOCAL_TESTING://本地网络正在测试
ll_localNet.setVisibility(View.VISIBLE);
tv_localNet.setText("Are testing the local network");
ad1.start();
ad2.stop();
break;
case Constant.Hanler.NET_PING://外网正在测试
ll_internet.setVisibility(View.VISIBLE);
tv_internet.setText("Are testing the outside network");
ad2.start();
break;
case Constant.Hanler.NET_DNS_TESTING://DNS正在测试
ll_dns.setVisibility(View.VISIBLE);
tv_dns.setText("Is testing the DNS configuration");
break;
case Constant.Hanler.EXTERNALSTORAGE_TESTING://
ll_source.setVisibility(View.VISIBLE);
tv_source.setText("Is testing of common resources");
break;
case Constant.Hanler.NET_TEST_RESULT:
if(isDns && isLocal && isPing){
tv_result.setVisibility(View.VISIBLE);
}
break;
default:
break;
}
};
};
@Override
public void setContentView() {
setContentView(R.layout.activity_net_test);
}
@Override
public void findView() {
ll_dns=(LinearLayout) findViewById(R.id.ll_dns);
ll_internet=(LinearLayout) findViewById(R.id.ll_internet);
ll_localNet=(LinearLayout) findViewById(R.id.ll_local_net);
ll_source=(LinearLayout) findViewById(R.id.ll_source);
tv_dns=(TextView) findViewById(R.id.tv_dns);
tv_internet=(TextView) findViewById(R.id.tv_internet);
tv_localNet=(TextView) findViewById(R.id.tv_local_net);
tv_source=(TextView) findViewById(R.id.tv_source);
tv_result=(TextView) findViewById(R.id.tv_result);
iv_test2=(ImageView) findViewById(R.id.iv_test_2);
iv_test3=(ImageView) findViewById(R.id.iv_test_3);
test1=(ImageView) findViewById(R.id.test1);
test2=(ImageView) findViewById(R.id.test2);
}
@Override
public void initData() {
setTitle("Network Test");
ad1=(AnimationDrawable) test1.getBackground();
ad2=(AnimationDrawable) test2.getBackground();
t=new Thread(new Runnable() {
@Override
public void run() {
//1.判断本地网络是否连接
handler.sendEmptyMessage(Constant.Hanler.NET_LOCAL_TESTING);
try {
t.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(Util.isInternetEnabled(NetTestActivity.this)){
handler.sendEmptyMessage(Constant.Hanler.NET_LOCAL_SUCCEEDED);
//2.开始判断外网是否连接
handler.sendEmptyMessage(Constant.Hanler.NET_PING);
try {
t.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String ip = "www.baidu.com";// ping 的地址,可以换成任何一种可靠的外网
String ip2="www.google.cn";
boolean ping1=ping(ip);
boolean ping2=ping(ip2);
if(ping1 || ping2){
handler.sendEmptyMessage(Constant.Hanler.NET_PING_SUCCEEDED);
}else{
handler.sendEmptyMessage(Constant.Hanler.NET_PING_FAIL);
}
//检查DNS设置
handler.sendEmptyMessage(Constant.Hanler.NET_DNS_TESTING);
try {
t.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
checkDNS();
//如果是wifi连接,显示wifi连接信号
handler.sendEmptyMessage(Constant.Hanler.EXTERNALSTORAGE_TESTING);
try {
t.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
testSDcard();
handler.sendEmptyMessage(Constant.Hanler.NET_TEST_RESULT);
}else{
handler.sendEmptyMessage(Constant.Hanler.NET_LOCAL_FAIL);
}
}
});
t.start();
}
@Override
public void setEventListener() {
}
private void checkDNS(){
if(WifiUtil.isEthConnected(this)){
String dns=Util.getEthernetMacAddress();
if(Util.isNullStr(dns)){
handler.sendEmptyMessage(Constant.Hanler.NET_DNS_FAIL);
}else{
handler.sendEmptyMessage(Constant.Hanler.NET_DNS_SUCCEEDED);
}
}else if(WifiUtil.isWifiConnected(this)){
String dns=Util.getWifiMacAddress(this);
if(Util.isNullStr(dns)){
handler.sendEmptyMessage(Constant.Hanler.NET_DNS_FAIL);
}else{
handler.sendEmptyMessage(Constant.Hanler.NET_DNS_SUCCEEDED);
}
}
}
private void testSDcard(){
if(Util.isSdcardMounted()){
if(Environment.getExternalStorageDirectory().canWrite()){
String path=Environment.getExternalStorageDirectory().getPath();
File f=new File(path+"/test");
try {
f.createNewFile();
f.delete();
handler.sendEmptyMessage(Constant.Hanler.EXTERNALSTORAGE_SUCCEEDED);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
handler.sendEmptyMessage(Constant.Hanler.EXTERNALSTORAGE_FAILED);
}
}else{
handler.sendEmptyMessage(Constant.Hanler.EXTERNALSTORAGE_FAILED);
}
}
private boolean ping(String ip) {
String result = null;
try {
Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);// ping网址3次
// 读取ping的内容,可以不加
/* InputStream input = p.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuffer stringBuffer = new StringBuffer();
String content = "";
while ((content = in.readLine()) != null) {
stringBuffer.append(content);
} */
// ping的状态
int status = p.waitFor();
if (status == 0) {
result = "success";
return true;
} else {
result = "failed";
}
} catch (IOException e) {
result = "IOException";
} catch (InterruptedException e) {
result = "InterruptedException";
} finally {
Log.d("----result---", "result = " + result);
}
return false;
}
}