Android5.0-7.0开机使用静态IP方法

1、打上下面补丁,自行修改IP信息

/rk3288_rel_5.1/frameworks/opt/net/ethernet$ git diff
diff --git a/java/com/android/server/ethernet/EthernetServiceImpl.java b/java/com/android/server/ethernet/EthernetServiceImpl.java
index bf4ab8e..ab7b334 100644
--- 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.util.Log;
 import android.util.PrintWriterPrinter;
 
@@ -49,6 +55,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;
 
@@ -61,7 +68,28 @@ 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 = "192.168.1.100";
+                int mNetmask = 24;
+                String mGateway = "192.168.1.1";
+                String mDns1 = "192.168.1.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);
 
@@ -221,4 +249,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;
+        }
+   }
 }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值