Prerequisite: The following involved in the content of the need for system permissions, that is, system applications, development of non-system app is Rao edge.
Ethernet settings are generally entered into the system native Settings app, into the Ethernet option to set. In some special occasions, the need to customize the app, do not directly call the system settings, then you have to implement the Ethernet dynamic and static settings.
Permissions:
<uses-permission android:name= "Android.permission.WRITE_SETTINGS"/>
<uses-permission android:name= "Android.permission.WRITE_SECURE_SETTINGS"/>
The Ethernetdevinfo class has the following members:
Private String Dev_name;
Private String ipaddr; IP Address
Private String netmask; Subnet mask
Private String GW; Gateway Settings
Private String DNS; DNS settings
private int mode; Mode settings: Static or dynamic
Private String hwaddr; MAC address
First, Get Ethernetmanager instance
Private Ethernetmanager Methmanager;
Private Ethernetdevinfo Mdevinfo;
Methmanager = Ethernetmanager.getinstance ();
Second, fill Mdevinfo
Static:
Mdevinfo = Methmanager.getsavedconfig ();
Mdevinfo.setconnectmode (ethernetdevinfo.ethernet_conn_mode_manual);
Mdevinfo.setipaddress ("XXX");
Mdevinfo.setnetmask ("XXX");
MDEVINFO.SETDNSADDR ("XXX");
Mdevinfo.setgateway ("XXX");
MDEVINFO.SETHWADDR ("XXX"); Getsavedconfig obtained MAC address is null, need to actively populate the address, otherwise enter system settings because address null crash
Dynamic:
Mdevinfo = Methmanager.getsavedconfig ();
Mdevinfo.setconnectmode (ETHERNETDEVINFO.ETHERNET_CONN_MODE_DHCP);
MDEVINFO.SETHWADDR ("XXX");
Third, update configuration information and enable Ethernet service
Methmanager.updatedevinfo (Mdevinfo);
Methmanager.setenabled (TRUE);
For more information, please refer to:
Frameworks/base/ethernet/java/android/net/ethernet/ethernetmanager.java
Frameworks/base/ethernet/java/android/net/ethernet/ethernetdevinfo.java