Android Ethernet Ethernet DHCP and static settings implementation

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.