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; }
可以通过以上方法组合使用,实现自己的需求。。第一篇博客!!!!!!
好开森!!!!!!!!!有木有!!!!!!!!!!!!!!!!!!!!啊哈哈哈哈!!!!!!!会不会有人发现我/疑惑 /抠鼻
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。