/** * 获取本机的Ip * * @return */ public String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e("WifiPreference IpAddress", ex.toString()); } return null; }
/** * 获取本机IP * * @return * @throws SocketException */ public static String getRealIp() throws SocketException { String localip = null;// 本地IP,如果没有配置外网IP则返回它 String netip = null;// 外网IP Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; boolean finded = false;// 是否找到外网IP while (netInterfaces.hasMoreElements() && !finded) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> address = ni.getInetAddresses(); while (address.hasMoreElements()) { ip = address.nextElement(); if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 外网IP netip = ip.getHostAddress(); finded = true; break; } else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 内网IP localip = ip.getHostAddress(); } } } if (netip != null && !"".equals(netip)) { return netip; } else { return localip; } }
//Android ping服务器命令 public boolean runPingIPprocess(String ip) { try { String pingStr = "ping -c 1 " + ip; Process p = Runtime.getRuntime().exec(pingStr); int status = p.waitFor(); if (status == 0) { return true; } else { return false; } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return false; }
public HttpURLConnection getConnection(String urlStr) throws IOException { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setConnectTimeout(5 * 1000); conn.setRequestProperty("Content-Type", "application/octet-stream"); return conn; }
//Android4.0以后不能再主线程中打开连接 public void testNetThread(final String url) { new Thread() { public void run() { try { Log.i(TAG, "url = " + url); HttpURLConnection htpConn = getConnection(url); htpConn.connect(); int code = htpConn.getResponseCode(); Log.i(TAG, "code = " + code); if (200 == code) {
//连接成功 } else { } } catch (IOException e) { e.printStackTrace(); } }; }.start(); }
/** 检测网络是否连接 */ public static boolean checkNetWork(Context context) { // 获取网络通讯类的实例 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); // 判断Android手机是否联网 NetworkInfo info = cm.getActiveNetworkInfo(); if (info != null && info.isConnected()) { return true; } return false; }
/** * 网络已经连接,然后去判断是wifi连接还是GPRS连接 设置一些自己的逻辑调用 */ public static int isNetworkAvailable(Context context) { // 得到网络连接信息 ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); State gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); if (gprs == State.CONNECTED || gprs == State.CONNECTING) {
//gprs网络 return NET_STATE_GPRS; } else if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
//wifi网络 return NET_STATE_WIFI; } return 0; }
/** 无网络连接时提示设置网络 */ public static void setNetWork(final Handler handler, final Context context) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("网络"); builder.setMessage("网络未连接"); builder.setPositiveButton("设置网络", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = null; // 判断手机系统的版本 即API大于10 就是3.0或以上版本 if (android.os.Build.VERSION.SDK_INT > 10) { // 进入网络连接设置界面 intent = new Intent(android.provider.Settings.ACTION_SETTINGS); } else { intent = new Intent(); ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings"); intent.setComponent(cn); intent.setAction("android.intent.action.VIEW"); } context.startActivity(intent); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { handler.sendEmptyMessage(MSG_CLOSE); } }); builder.show(); }