Android 10 默认配置以太网静态IP

代码路径:
device/sprd/sharkl3/uis8581a2h10/system.prop
frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetServiceImpl.java
在system.prop文件中添加一个属性控制是否配置静态IP

diff --git a/system.prop b/system.prop
index fce9347..e8e5f81 100755
--- a/system.prop
+++ b/system.prop
@@ -62,3 +62,4 @@ ro.rk.displayd.enable=false
 ro.boot.wificountrycode=CN
 #persist.sys.resolution.aux=1920x1080p60
 persist.sys.framebuffer.main=1920x1080
+persist.sys.static.enable=1

在EthernetServiceImpl.java文件中EthernetServiceImpl构造函数中默认配置IP

diff --git a/java/com/android/server/ethernet/EthernetServiceImpl.java b/java/com/android/server/ethernet/EthernetServiceImpl.java
index bd91e76..aa391fa 100755
--- a/java/com/android/server/ethernet/EthernetServiceImpl.java
+++ b/java/com/android/server/ethernet/EthernetServiceImpl.java
@@ -23,11 +23,17 @@ import android.net.IEthernetServiceListener;
 import android.net.IpConfiguration;
 import android.net.IpConfiguration.IpAssignment;
 import android.net.IpConfiguration.ProxySettings;
+import java.net.InetAddress;
+import java.net.Inet4Address;
+import android.net.LinkAddress;
+import android.net.NetworkUtils;
+import android.net.StaticIpConfiguration;
 import android.os.Binder;
 import android.os.Handler;
 import android.os.HandlerThread;
 import android.os.RemoteCallbackList;
 import android.os.RemoteException;
+import android.os.SystemProperties;
 import android.provider.Settings;
 import android.util.Log;
 import android.util.PrintWriterPrinter;
@@ -50,6 +56,7 @@ public class EthernetServiceImpl extends IEthernetManager.Stub {
 
     private final Context mContext;
     private final EthernetConfigStore mEthernetConfigStore;
+	private final StaticIpConfiguration mStaticIpConfiguration;
     private final AtomicBoolean mStarted = new AtomicBoolean(false);
     private IpConfiguration mIpConfiguration;
 
@@ -62,7 +69,29 @@ public class EthernetServiceImpl extends IEthernetManager.Stub {
         mContext = context;
         Log.i(TAG, "Creating EthernetConfigStore");
         mEthernetConfigStore = new EthernetConfigStore();
-        mIpConfiguration = mEthernetConfigStore.readIpAndProxyConfigurations();
+        mStaticIpConfiguration = new StaticIpConfiguration();
+        if (SystemProperties.get("persist.sys.static.enable","1").equals("1")){                             //静态控制
+                String mIpAddress = "10.10.10.88";
+                int mNetmask = 24;
+                String mGateway = "10.10.10.1";
+                String mDns1 = "10.10.10.1";
+                String mDns2 = "8.8.8.8";
+
+                Inet4Address inetAddr = getIPv4Address(mIpAddress);
+                int prefixLength = mNetmask;
+                InetAddress gatewayAddr =getIPv4Address(mGateway); 
+                InetAddress dnsAddr = getIPv4Address(mDns1);
+                
+                mStaticIpConfiguration.ipAddress = new LinkAddress(inetAddr, prefixLength);
+                mStaticIpConfiguration.gateway=gatewayAddr;
+                mStaticIpConfiguration.dnsServers.add(dnsAddr);
+                mStaticIpConfiguration.dnsServers.add(getIPv4Address(mDns2));
+                mIpConfiguration = new IpConfiguration(IpAssignment.STATIC, ProxySettings.NONE,mStaticIpConfiguration,null);
+                mEthernetConfigStore.writeIpAndProxyConfigurations(mIpConfiguration);
+                //SystemProperties.set("persist.sys.static.enable","0");                                         //首次开机用静态,若要写死每次开机都是静态,可以将此句屏蔽掉  
+        } else {
+                mIpConfiguration = mEthernetConfigStore.readIpAndProxyConfigurations();
+        }
 
         Log.i(TAG, "Read stored IP configuration: " + mIpConfiguration);
 
@@ -271,4 +300,12 @@ public class EthernetServiceImpl extends IEthernetManager.Stub {
         mHandler.dump(new PrintWriterPrinter(pw), "EthernetServiceImpl");
         pw.decreaseIndent();
     }
+	
+	private Inet4Address getIPv4Address(String text) {
+        try {
+                return (Inet4Address) NetworkUtils.numericToInetAddress(text);
+        } catch (IllegalArgumentException|ClassCastException e) {
+                return null;
+        }
+   }
 }
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值