2020年2月24日 星期一

android 9 sdk 更改 ethernet ip

Android.mk

LOCAL_CERTIFICATE := platform
LOCAL_PRIVATE_PLATFORM_APIS := true

AndroidManifest.xml
android:sharedUserId="android.uid.system"



source code:


import java.net.*;
import android.net.EthernetManager;
import android.net.IpConfiguration;
import android.net.StaticIpConfiguration;
import android.net.NetworkUtils;
import android.net.IpConfiguration.IpAssignment;

public void setEthernetIP(Context context, String mode, String ipAddress, String netmask,
                                     String gateway, String dns1, String dns2) {
        mEthernetManager = (EthernetManager) context.getSystemService(Context.ETHERNET_SERVICE);
		mIpConfiguration = new IpConfiguration();
		mIpConfiguration = mEthernetManager.getConfiguration("eth0");
		
		StaticIpConfiguration staticConfig = new StaticIpConfiguration();
		
		Inet4Address inetAddr = null;
		inetAddr = (Inet4Address) NetworkUtils.numericToInetAddress("192.168.66.10");
		staticConfig.ipAddress = new LinkAddress(inetAddr, 24);
		staticConfig.gateway = (Inet4Address) NetworkUtils.numericToInetAddress("192.168.66.1");
		staticConfig.dnsServers.add((Inet4Address) NetworkUtils.numericToInetAddress("8.8.8.8"));
		
		mIpConfiguration.setIpAssignment(IpAssignment.STATIC);
		mIpConfiguration.setStaticIpConfiguration(staticConfig);
		
		mEthernetManager.setConfiguration("eth0", mIpConfiguration);
    }

沒有留言:

張貼留言