ConnectivityManager mService = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); EthernetManager mEthManager = (EthernetManager) getSystemService(Context.ETHERNET_SERVICE); if(mEthManager.getState() == EthernetManager.ETHERNET_STATE_ENABLED) { EthernetDevInfo mInterfaceInfo = mEthManager.getSavedConfig(); String mIp; String mMask; String mGw; String mDns; mIp = "192.168.0.118"; mMask = "255.255.255.0"; mGw = "192.168.0.1"; mDns = "192.168.0.1"; mInterfaceInfo.setConnectMode(EthernetDevInfo.ETHERNET_CONN_MODE_MANUAL); mInterfaceInfo.setIpAddress(mIp); mInterfaceInfo.setNetMask(mMask); mInterfaceInfo.setDnsAddr(mDns); mInterfaceInfo.setGateWay(mGw); try{ mEthManager.updateDevInfo(mInterfaceInfo); Thread.sleep(500); }catch(Exception e){ e.printStackTrace(); } } else { Toast.makeText(this, "Ethernet state disabled!", 5000).show(); } 测试随便写的,已经验证。大家根据需要自己改吧。(需要源码环境编译)
方法2:
package com.android.jun; import java.io.BufferedReader; import java.io.InputStreamReader; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Bundle; import android.provider.Settings; import android.webkit.WebView; import android.widget.Toast; public class TextSettingIp extends Activity { /** Called when the activity is first created. */ // 定义侦听端口号 final int SERVER_PORT = 30000; public static void setIpAssignment(String assign, WifiConfiguration wifiConf) throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { 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 { Field f = obj.getClass().getField(name); f.set(obj, Enum.valueOf((Class<Enum>) f.getType(), value)); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); WifiConfiguration wifiConf = null; WifiManager 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", 5555).show(); break; } } 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", 5555).show(); wifiManager.updateNetwork(wifiConf); // apply the setting } } 需要权限 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" > </uses-permission> <uses-permission android:name="android.permission.INTERNET" > </uses-permission> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
转载自:
http://www.eoeandroid.com/thread-233453-1-1.html
http://www.eoeandroid.com/thread-248941-1-1.html