android 8.1 framework层修改以太网静态ip功能

Android5.0基础上到Android7.0,Android都自带了以太网的设置功能,基本上都是将ip地址和网关之类的信息通过StaticIpConfiguration该对象实现联网功能。到了Android 8.1 如果还是照着7.0的来写,可能会出现配置了基本信息,却不能通过静态ip上网的情况。不要问我怎么知道的,因为我就是碰到这个 坑了。

 我默认你们已经知道怎么去实现设置ip的功能。在这个基础上,我们只需要修改一下framework层的代码就好。

设置静态ip的代码:

Editor etherEditor = etherModePreferences.edit();
etherEditor.putString("ether_mode", "STATIC");
etherEditor.commit();
try {
	StaticIpConfiguration staticIpConfiguration = new StaticIpConfiguration();
       InetAddress mIpAddr = NetworkUtils.numericToInetAddress(mIpaddr.getText().toString());
String[] strs = mMask.getText().toString().split("\\.");
	int count = 0;
	for(String str : strs){
		if(str.equals("255")){
		count++;
					}
				}
	int prefixLength = count*8;
	LinkAddress mIpAddress = new LinkAddress(mIpAddr,prefixLength);
InetAddress mGateway = NetworkUtils.numericToInetAddress(mGw.getText().toString());
ArrayList<InetAddress> mDnsServers = new ArrayList<InetAddress>();
				mDnsServers.add(NetworkUtils.numericToInetAddress(mDns1.getText().toString()));
				mDnsServers.add(NetworkUtils.numericToInetAddress(mDns2.getText().toString()));
				
staticIpConfiguration.ipAddress = mIpAddress;
staticIpConfiguration.gateway = mGateway;
staticIpConfiguration.dnsServers.addAll(mDnsServers);
				
config = new IpConfiguration(IpAssignment.STATIC, ProxySettings.NONE, staticIpConfiguration, ProxyInfo.buildDirectProxy(null,0));
	mEthManager.setConfiguration(config);
	}catch (Exception e) {
	        e.printStackTrace();  
	  }

[点击并拖拽以移动]
​

 通过上面的代码可以把静态ip地址设置,但是你发现你只写了这写方法并不能用以太网实现联网功能。这个时候我们就需要修改

frameworks\opt\net\ethernet\java\com\android\server\ethernet目录下面的EthernetNetworkFactory文件,代码如下:

public void startIpManager() {
        if (DBG) {
            Log.d(TAG, String.format("starting IpManager(%s): mNetworkInfo=%s", mIface,
                    mNetworkInfo));
        }

        LinkProperties linkProperties;

        IpConfiguration config = mEthernetManager.getConfiguration();

        if (config.getIpAssignment() == IpAssignment.STATIC) {
            if (!setStaticIpAddress(config.getStaticIpConfiguration())) {
                // We've already logged an error.
                return;
            }
            linkProperties = config.getStaticIpConfiguration().toLinkProperties(mIface);
      /*   } else {
            mNetworkInfo.setDetailedState(DetailedState.OBTAINING_IPADDR, null, mHwAddr);
            IpManager.Callback ipmCallback = new IpManager.Callback() {
                @Override
                public void onProvisioningSuccess(LinkProperties newLp) {
                    mHandler.post(() -> onIpLayerStarted(newLp));
                }

                @Override
                public void onProvisioningFailure(LinkProperties newLp) {
                    mHandler.post(() -> onIpLayerStopped(newLp));
                }

                @Override
                public void onLinkPropertiesChange(LinkProperties newLp) {
                    mHandler.post(() -> updateLinkProperties(newLp));
                }
            };

            stopIpManager();
            mIpManager = new IpManager(mContext, mIface, ipmCallback); */

            if (config.getProxySettings() == ProxySettings.STATIC ||
                    config.getProxySettings() == ProxySettings.PAC) {
                // mIpManager.setHttpProxy(config.getHttpProxy());
                linkProperties.setHttpProxy(config.getHttpProxy());
            }

            /* final String tcpBufferSizes = mContext.getResources().getString(
                    com.android.internal.R.string.config_ethernet_tcp_buffers); */
            String tcpBufferSizes = mContext.getResources().getString(
                    com.android.internal.R.string.config_ethernet_tcp_buffers);		
            /* if (!TextUtils.isEmpty(tcpBufferSizes)) {
                mIpManager.setTcpBufferSizes(tcpBufferSizes);
            } */
            if (TextUtils.isEmpty(tcpBufferSizes) == false) {
                linkProperties.setTcpBufferSizes(tcpBufferSizes);
            }
        } else {
            mNetworkInfo.setDetailedState(DetailedState.OBTAINING_IPADDR, null, mHwAddr);
        }
           /*  final ProvisioningConfiguration provisioningConfiguration =
                    mIpManager.buildProvisioningConfiguration()
                            .withProvisioningTimeoutMs(0)
                            .build();
            mIpManager.startProvisioning(provisioningConfiguration); */
        // }
        IpManager.Callback ipmCallback = new IpManager.Callback() {
            @Override
            public void onProvisioningSuccess(LinkProperties newLp) {
                mHandler.post(() -> onIpLayerStarted(newLp));
            }

            @Override
            public void onProvisioningFailure(LinkProperties newLp) {
                mHandler.post(() -> onIpLayerStopped(newLp));
            }

            @Override
            public void onLinkPropertiesChange(LinkProperties newLp) {
                mHandler.post(() -> updateLinkProperties(newLp));
            }
        };
        stopIpManager();
        mIpManager = new IpManager(mContext, mIface, ipmCallback);

        if (config.getProxySettings() == ProxySettings.STATIC ||
                config.getProxySettings() == ProxySettings.PAC) {
            mIpManager.setHttpProxy(config.getHttpProxy());
        }

        final String tcpBufferSizes = mContext.getResources().getString(
                com.android.internal.R.string.config_ethernet_tcp_buffers);
        if (!TextUtils.isEmpty(tcpBufferSizes)) {
            mIpManager.setTcpBufferSizes(tcpBufferSizes);
        }

        if (config.getIpAssignment() == IpAssignment.STATIC) {
            mIpManager.startProvisioning(config.getStaticIpConfiguration());
        } else {
            final ProvisioningConfiguration provisioningConfiguration =
                    mIpManager.buildProvisioningConfiguration()
                            .withProvisioningTimeoutMs(0)
                            .build();
            mIpManager.startProvisioning(provisioningConfiguration);

        }
    }

通过设置修改以上代码就可以实现上网功能了。希望能对需要的人有用。

猜你喜欢

转载自blog.csdn.net/qq_33796069/article/details/86636296