android ping检测网络连接,android 判断网络连接、sim卡信息以及ping操作是否网络连接正常...

//判断是否为wifi连接

public boolean isWifiConnected(Context context) {

if (context != null) {

ConnectivityManager mConnectivityManager = (ConnectivityManager) context

.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo mWiFiNetworkInfo = mConnectivityManager

.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWiFiNetworkInfo != null) {

return mWiFiNetworkInfo.isAvailable();

}

}

return false;

}

//判断是否为网络连接是否正常

public boolean isNetworkConnected(Context context) {

if (context != null) {

ConnectivityManager mConnectivityManager = (ConnectivityManager) context

.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo mNetworkInfo = mConnectivityManager

.getActiveNetworkInfo();

if (mNetworkInfo != null) {

return mNetworkInfo.isAvailable();

}

}

return false;

}

//判断网络连接类型

public static int getConnectedType(Context context) {

if (context != null) {

ConnectivityManager mConnectivityManager = (ConnectivityManager) context

.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo mNetworkInfo = mConnectivityManager

.getActiveNetworkInfo();

if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {

return mNetworkInfo.getType();

}

}

return -1;

}

//判断手机sim卡类型

public static String get(Context context) {

TelephonyManager telManager = (TelephonyManager) context

.getSystemService(Context.TELEPHONY_SERVICE);

String operator = telManager.getSimOperator();

if (operator != null) {

if (operator.equals("46000") || operator.equals("46002")) {

// 中国移动

return "移动";

} else if (operator.equals("46001")) {

return "联通";

// 中国联通

} else if (operator.equals("46003")) {

return "电信";

// 中国电信

}

}

return "";

}

//判断手机是否能正常上网 进行ping操作, 即使判断手机连接了wifi了(热点没有上网),手机还是不能上网

private static final boolean ping() {

String result = null;

try {

String ip = "www.baidu.com";// 除非百度挂了,否则用这个应该没问题~

Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);//ping3次

// 读取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);

}

Log.i("TTT", "result content : " + stringBuffer.toString());

// PING的状态

int status = p.waitFor();

if (status == 0) {

result = "successful~";

return true;

} else {

result = "failed~ cannot reach the IP address";

}

} catch (IOException e) {

result = "failed~ IOException";

} catch (InterruptedException e) {

result = "failed~ InterruptedException";

} finally {

Log.i("TTT", "result = " + result);

}

return false;

}

可以通过以上方法组合使用,实现自己的需求。。第一篇博客!!!!!!

好开森!!!!!!!!!有木有!!!!!!!!!!!!!!!!!!!!啊哈哈哈哈!!!!!!!会不会有人发现我/疑惑 /抠鼻

原文:http://bran08.blog.51cto.com/5268058/1432740

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值