//查看Wifi地址 public String getWifiIpAddress() { //获取wifi服务 WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); //判断wifi是否开启 if (!wifiManager.isWifiEnabled()) { wifiManager.setWifiEnabled(true); } WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); String ip = intToIp(ipAddress); Log.d("wuc", "wifiIp = " + ip); return ip; } //获取GPRS本地ip地址 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() && !inetAddress.isLinkLocalAddress()) { Log.d("wuc", "gprs = " + inetAddress.getHostAddress().toString()); return inetAddress.getHostAddress().toString(); }} } } catch (SocketException ex) { Log.e("WifiPreference IpAddress", ex.toString()); } return null; } private String intToIp(int i) { return (i & 0xFF ) + "." + ((i >> 8 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF) + "." + ( i >> 24 & 0xFF) ; }日志信息09-10 14:58:06.288 12190-12190/com.example.administrator.newtext D/wuc: gprs = 10.96.76.60 09-10 14:58:15.488 12190-12190/com.example.administrator.newtext D/wuc: wifiIp = 192.168.1.103修改wifi地址网上找的-------------------------@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo connectionInfo = wifiManager.getConnectionInfo(); List<WifiConfiguration> configuredNetworks = wifiManager .getConfiguredNetworks(); for (WifiConfiguration conf : configuredNetworks) { if (conf.networkId == connectionInfo.getNetworkId()) { wifiConf = conf; Toast.makeText(this, "11111", Toast.LENGTH_SHORT).show(); break; } } Log.d("wuc", "wifiConfout = " + wifiConf); try { setIpAssignment("STATIC", wifiConf); setIpAddress(InetAddress.getByName("192.168.0.110"), 24, wifiConf); setGateway(InetAddress.getByName("255.255.255.0"), wifiConf); setDNS(InetAddress.getByName("255.255.255.0"), wifiConf); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Toast.makeText(this, "1121", Toast.LENGTH_SHORT).show(); wifiManager.updateNetwork(wifiConf); // apply the settingpublic static void setIpAssignment(String assign, WifiConfiguration wifiConf) throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { Log.d("wuc", "wifiConfin = " + wifiConf); setEnumField(wifiConf, assign, "ipAssignment"); } public static void setIpAddress(InetAddress addr, int prefixLength, WifiConfiguration wifiConf) throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, ClassNotFoundException, InstantiationException, InvocationTargetException { Object linkProperties = getField(wifiConf, "linkProperties"); if (linkProperties == null) return; Class laClass = Class.forName("android.net.LinkAddress"); Constructor laConstructor = laClass.getConstructor(new Class[]{ InetAddress.class, int.class}); Object linkAddress = laConstructor.newInstance(addr, prefixLength); ArrayList mLinkAddresses = (ArrayList) getDeclaredField(linkProperties, "mLinkAddresses"); mLinkAddresses.clear(); mLinkAddresses.add(linkAddress); } public static void setGateway(InetAddress gateway, WifiConfiguration wifiConf) throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InstantiationException, InvocationTargetException { Object linkProperties = getField(wifiConf, "linkProperties"); if (linkProperties == null) return; Class routeInfoClass = Class.forName("android.net.RouteInfo"); Constructor routeInfoConstructor = routeInfoClass .getConstructor(new Class[]{InetAddress.class}); Object routeInfo = routeInfoConstructor.newInstance(gateway); ArrayList mRoutes = (ArrayList) getDeclaredField(linkProperties, "mRoutes"); mRoutes.clear(); mRoutes.add(routeInfo); } public static void setDNS(InetAddress dns, WifiConfiguration wifiConf) throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { Object linkProperties = getField(wifiConf, "linkProperties"); if (linkProperties == null) return; ArrayList<InetAddress> mDnses = (ArrayList<InetAddress>) getDeclaredField( linkProperties, "mDnses"); mDnses.clear(); // or add a new dns address , here I just want to // replace DNS1 mDnses.add(dns); } public static Object getField(Object obj, String name) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field f = obj.getClass().getField(name); Object out = f.get(obj); return out; } public static Object getDeclaredField(Object obj, String name) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field f = obj.getClass().getDeclaredField(name); f.setAccessible(true); Object out = f.get(obj); return out; } public static void setEnumField(Object obj, String value, String name) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Log.d("wuc", "wifiConfinin = " + obj); Field f = obj.getClass().getField(name); f.set(obj, Enum.valueOf((Class<Enum>) f.getType(), value)); }}但是这个wifi修改地址没成功。提示:java.lang.NoSuchFieldException: ipAssignment难道是ipAssignment名称不对?看网上说getField换成getDeclaredField提示:java.lang.NoSuchFieldException: No field ipAssignment in class Landroid/net/wifi/WifiConfiguration; (declaration of 'android.net.wifi.WifiConfiguration' appears in /system/framework/framework.jar)不知道是哪错了求大神指导
Android 获取手机Wifi地址和Gprs地址,反射修改Wifi地址
最新推荐文章于 2024-08-17 00:45:31 发布