当前位置:   article > 正文

Android5.0-7.0开机使用静态IP方法_android framework/opt 调用android.net.networkutils

android framework/opt 调用android.net.networkutils

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

  1. /rk3288_rel_5.1/frameworks/opt/net/ethernet$ git diff
  2. diff --git a/java/com/android/server/ethernet/EthernetServiceImpl.java b/java/com/android/server/ethernet/EthernetServiceImpl.java
  3. index bf4ab8e..ab7b334 100644
  4. --- a/java/com/android/server/ethernet/EthernetServiceImpl.java
  5. +++ b/java/com/android/server/ethernet/EthernetServiceImpl.java
  6. @@ -23,11 +23,17 @@ import android.net.IEthernetServiceListener;
  7.  import android.net.IpConfiguration;
  8.  import android.net.IpConfiguration.IpAssignment;
  9.  import android.net.IpConfiguration.ProxySettings;
  10. +import java.net.InetAddress;
  11. +import java.net.Inet4Address;
  12. +import android.net.LinkAddress;
  13. +import android.net.NetworkUtils;
  14. +import android.net.StaticIpConfiguration;
  15.  import android.os.Binder;
  16.  import android.os.Handler;
  17.  import android.os.HandlerThread;
  18.  import android.os.RemoteCallbackList;
  19.  import android.os.RemoteException;
  20. +import android.os.SystemProperties;
  21.  import android.util.Log;
  22.  import android.util.PrintWriterPrinter;
  23.  
  24. @@ -49,6 +55,7 @@ public class EthernetServiceImpl extends IEthernetManager.Stub {
  25.  
  26.      private final Context mContext;
  27.      private final EthernetConfigStore mEthernetConfigStore;
  28. +    private final StaticIpConfiguration mStaticIpConfiguration;
  29.      private final AtomicBoolean mStarted = new AtomicBoolean(false);
  30.      private IpConfiguration mIpConfiguration;
  31.  
  32. @@ -61,7 +68,28 @@ public class EthernetServiceImpl extends IEthernetManager.Stub {
  33.          mContext = context;
  34.          Log.i(TAG, "Creating EthernetConfigStore");
  35.          mEthernetConfigStore = new EthernetConfigStore();
  36. -        mIpConfiguration = mEthernetConfigStore.readIpAndProxyConfigurations();
  37. +        mStaticIpConfiguration = new StaticIpConfiguration();
  38. +        if (SystemProperties.get("persist.sys.static.enable","1").equals("1")){                             //静态控制
  39. +                String mIpAddress = "192.168.1.100";
  40. +                int mNetmask = 24;
  41. +                String mGateway = "192.168.1.1";
  42. +                String mDns1 = "192.168.1.1";
  43. +                String mDns2 = "8.8.8.8";
  44. +
  45. +                Inet4Address inetAddr = getIPv4Address(mIpAddress);
  46. +                int prefixLength = mNetmask;
  47. +                InetAddress gatewayAddr =getIPv4Address(mGateway); 
  48. +                InetAddress dnsAddr = getIPv4Address(mDns1);
  49. +                
  50. +                mStaticIpConfiguration.ipAddress = new LinkAddress(inetAddr, prefixLength);
  51. +                mStaticIpConfiguration.gateway=gatewayAddr;
  52. +                mStaticIpConfiguration.dnsServers.add(dnsAddr);
  53. +                mStaticIpConfiguration.dnsServers.add(getIPv4Address(mDns2));
  54. +                mIpConfiguration = new IpConfiguration(IpAssignment.STATIC, ProxySettings.NONE,mStaticIpConfiguration,null);
  55. +                mEthernetConfigStore.writeIpAndProxyConfigurations(mIpConfiguration);
  56. +                SystemProperties.set("persist.sys.static.enable","0");                                         //首次开机用静态,若要写死每次开机都是静态,可以将此句屏蔽掉  
  57. +        } else {
  58. +                mIpConfiguration = mEthernetConfigStore.readIpAndProxyConfigurations();
  59. +        }
  60.  
  61.          Log.i(TAG, "Read stored IP configuration: " + mIpConfiguration);
  62.  
  63. @@ -221,4 +249,12 @@ public class EthernetServiceImpl extends IEthernetManager.Stub {
  64.          mHandler.dump(new PrintWriterPrinter(pw), "EthernetServiceImpl");
  65.          pw.decreaseIndent();
  66.      }
  67. +    
  68. +    private Inet4Address getIPv4Address(String text) {
  69. +        try {
  70. +                return (Inet4Address) NetworkUtils.numericToInetAddress(text);
  71. +        } catch (IllegalArgumentException|ClassCastException e) {
  72. +                return null;
  73. +        }
  74. +   }
  75.  }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/527998
推荐阅读
相关标签
  

闽ICP备14008679号