編輯:關於Android編程
部分來自網絡: /** * 獲取移動設備本地IP * @return */ protected static InetAddress getLocalInetAddress() { InetAddress ip = null; try { //列舉 Enumerationen_netInterface = NetworkInterface.getNetworkInterfaces(); while (en_netInterface.hasMoreElements()) {//是否還有元素 NetworkInterface ni = (NetworkInterface) en_netInterface.nextElement();//得到下一個元素 Enumeration en_ip = ni.getInetAddresses();//得到一個ip地址的列舉 while (en_ip.hasMoreElements()) { ip = en_ip.nextElement(); if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) break; else ip = null; } if (ip != null) { break; } } } catch (SocketException e) { e.printStackTrace(); } return ip; }
/** * 獲取本地IP * @return */ public static String getLocalIpAddress() { try { for (Enumerationen = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { ex.printStackTrace(); } return null; }
/** * 根據IP地址獲取MAC地址 * @return */ @SuppressLint({ "NewApi", "DefaultLocale" }) public static String getMacAddress(){ String strMacAddr = null; try { //獲得IpD地址 InetAddress ip = getLocalInetAddress(); byte[] b = NetworkInterface.getByInetAddress(ip).getHardwareAddress(); StringBuffer buffer = new StringBuffer(); for (int i = 0; i < b.length; i++) { if (i != 0) { buffer.append(':'); } String str = Integer.toHexString(b[i] & 0xFF); buffer.append(str.length() == 1 ? 0 + str : str); } strMacAddr = buffer.toString().toUpperCase(); } catch (Exception e) { } return strMacAddr; }
/** * 根據IP獲取本地Mac * @param context * @return */ public static String getLocalMacAddressFromIp(Context context) { String mac_s= ""; try { byte[] mac; NetworkInterface ne=NetworkInterface.getByInetAddress(InetAddress.getByName(getLocalIpAddress())); mac = ne.getHardwareAddress(); mac_s = byte2hex(mac); } catch (Exception e) { e.printStackTrace(); } return mac_s; } /** * 二進制轉十六進制 * @param b * @return */ public static String byte2hex(byte[] b) { StringBuffer hs = new StringBuffer(b.length); String stmp = ""; int len = b.length; for (int n = 0; n < len; n++) { stmp = Integer.toHexString(b[n] & 0xFF); if (stmp.length() == 1) hs = hs.append("0").append(stmp); else { hs = hs.append(stmp); } } return String.valueOf(hs); }
/** * 根據busybox獲取本地Mac * @return */ public static String getLocalMacAddressFromBusybox(){ String result = ""; String Mac = ""; result = callCmd("busybox ifconfig","HWaddr"); //如果返回的result == null,則說明網絡不可取 if(result==null){ return "網絡異常"; } //對該行數據進行解析 //例如:eth0 Link encap:Ethernet HWaddr 00:16:E8:3E:DF:67 if(result.length()>0 && result.contains("HWaddr")==true){ Mac = result.substring(result.indexOf("HWaddr")+6, result.length()-1); result = Mac; } return result; } private static String callCmd(String cmd,String filter) { String result = ""; String line = ""; try { Process proc = Runtime.getRuntime().exec(cmd); InputStreamReader is = new InputStreamReader(proc.getInputStream()); BufferedReader br = new BufferedReader (is); while ((line = br.readLine ()) != null && line.contains(filter)== false) { result += line; } result = line; } catch(Exception e) { e.printStackTrace(); } return result; }
/** * 根據wifi信息獲取本地mac * @param context * @return */ public static String getLocalMacAddressFromWifiInfo(Context context){ WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo winfo = wifi.getConnectionInfo(); String mac = winfo.getMacAddress(); return mac; }
這是個很簡單的問題,但每次隔一段時間後使用起來總是會出點亂子。這裡記錄下Logcat的步驟:1,在Activity裡申明tag變量(名字其實是隨便的,如下:private
之前小編說過刷第三方的系統包,需要用到第三方recovery才可以刷入。之前已經為大家帶來過“小米5刷入第三方Recovery教程”
效果演示需求和技術分析RecyclerView Item拖拽排序::長按RecyclerView的Item或者觸摸Item的某個按鈕。 RecyclerView Item
在Android 5.0之前,雖然也有手機是雙卡手機,但是雙卡方案都是ODM廠商自己做的,或者是有芯片廠商提供的源碼完成。因此,如果要在4.4甚至更早之前的手機上獲取雙卡