android5.1 增加ethernet设置(DHCP与Static ip)

2024-02-17 08:32

本文主要是介绍android5.1 增加ethernet设置(DHCP与Static ip),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://www.07net01.com/program/2016/03/1391773.html

[摘要:android5.0以上的体系自带了ethernet service,默许开机便会启动,默许ip猎取体式格局是静态分派,那里纪录下android5.1增添ethernet设置界里设置ip猎取体式格局及开闭. 起首是界里圆里] 

android5.0以上的系统自带了ethernet service,默认开机就会启动,默认ip获取方式是动态分配,这里记录下android5.1增加ethernet设置界面设置ip获取方式及开关.

首先是界面方面要修改Settings增加ethernet设置界面

修改文件packages/apps/Settings/res/xml/dashboard_categories.xml在蓝牙后面加上如下代码

<!-- ethernet add by hclydao--><dashboard-tileandroid:id="@+id/ethernet_settings"android:icon="@drawable/ic_settings_dock"android:fragment="com.android.settings.ethernet.EthernetSettings"android:title="@string/ethernet_settings" />

其中的EthernetSettings后面进行说明,然后增加string修改文件packages/apps/Settings/res/values/strings.xml

增加如下内容

<!-- Eth settings title add by hclydao--><string name="ethernet_settings">Ethernet</string><!-- Ethernet configuration dialog add by hcldyao--><string name="eth_config_title">Configure Ethernet device</string><string name="eth_setting">Ethernet</string><string name="eth_dev_list">Ethernet Devices:</string><string name="eth_con_type">Connection Type</string><string name="eth_con_type_dhcp">DHCP</string><string name="eth_con_type_manual">Static IP</string><string name="eth_dns">DNS address</string><string name="eth_gw">Gateway address</string><string name="eth_ipaddr">IP address</string><string name="eth_quick_toggle_title">Ethernet</string><string name="eth_quick_toggle_summary">Turn on Ethernet</string><string name="eth_conf_perf_title">Ethernet configuration</string><string name="eth_conf_summary">Configure Ethernet devices</string><string name="eth_mask">Netmask</string><string name="eth_toggle_summary_off">Turn off Ethernet</string><string name="eth_toggle_summary_on">Turn on Ethernet</string><string name="eth_settings_error">Failed to set: Please enter the valid characters 0~255</string><string name="eth_settings_empty">can\'t be empty</string><!-- Label for the <a target=_blank href="http://www.07net01.com/tags-Network-0.html" target="_blank" class="infotextkey" style="box-sizing: border-box; background-color: transparent; color: rgb(66, 139, 202);">Network</a> prefix of the network [CHAR LIMIT=25]--><string name="eth_network_prefix_length">Network prefix length</string>
这是我修改完成后所要加的所有的string

接着修改文件packages/apps/Settings/src/com/android/settings/SettingsActivity.Java增加

import com.android.settings.ethernet.EthernetSettings;//add by hclydao

然后在R.id.bluetooth_settings,后增加

R.id.ethernet_settings,//add by hclydao

在BluetoothSettings.class.getName(),后增加

EthernetSettings.class.getName(),//add by hclydao

接着修改文件packages/apps/Settings/src/com/android/settings/Settings.java

在public static class WirelessSettingsActivity extends SettingsActivity { /* empty */ }后增加

public static class EthernetSettingsActivity extends SettingsActivity { /* empty */ } //add by hclydao

这里面应该是声明与继承关系

接着修改
packages/apps/Settings/AndroidManifest.xml

 <activity android:name="Settings$EthernetSettingsActivity"android:label="@string/ethernet_settings"android:taskAffinity=""><intent-filter><action android:name="android.intent.action.MAIN" /><action android:name="com.android.settings.ETHERNET_SETTINGS" /><action android:name="android.settings.ETHERNET_SETTINGS" /><category android:name="android.intent.category.DEFAULT" /><category android:name="android.intent.category.VOICE_LAUNCH" /><category android:name="com.android.settings.SHORTCUT" /></intent-filter><meta-data android:name="com.android.settings.FRAGMENT_CLASS"android:value=http://www.ithao123.cn/"com.android.settings.ethernet.EthernetSettings" /><meta-data android:name="com.android.settings.TOP_LEVEL_HEADER_ID" android:resource="@id/ethernet_settings" style="box-sizing: border-box;"><meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED" android:value="true" style="box-sizing: border-box;"></meta-data></meta-data>

这三个文件按照WIFI的代码改就行了。
然后增加点击进去后的布局文件,增加
packages/apps/Settings/res/xml/ethernet_settings.xml

内容如下

<?xml <a target=_blank href="http://www.07net01.com/tags-version-0.html" target="_blank" class="infotextkey" style="box-sizing: border-box; background-color: transparent; color: rgb(66, 139, 202);">version</a>="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source ProjectLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, <a target=_blank href="http://www.07net01.com/tags-software-0.html" target="_blank" class="infotextkey" style="box-sizing: border-box; background-color: transparent; color: rgb(66, 139, 202);">software</a>distributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
--><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"android:title="@string/ethernet_settings"xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"><Preferenceandroid:title="@string/eth_conf_perf_title"android:summary="@string/eth_conf_summary"android:key="ETHERNET_CONFIG"android:persistent="false" />
</PreferenceScreen>
然后增加了packages/apps/Settings/src/com/android/settings/ethernet/EthernetSettings.java

这是我最终的代码,这里就不贴上来了,下面我会给下载地址
编译后效果应该是这个样子的:

\

点击进去后会有一个Dialog布局,需要增加文件packages/apps/Settings/res/layout/eth_configure.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:padding="8dip"android:orientation="vertical"><LinearLayoutandroid:id="@+id/table"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"></LinearLayout><!-- Connection type --><TextView android:id="@+id/eth_con_type"style="?android:attr/textAppearanceSmall"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="8dip"android:text="@string/eth_con_type" /><RadioGroup android:id="@+id/con_type"android:layout_width="fill_parent"android:layout_height="wrap_content"><RadioButton android:id="@+id/dhcp_radio"style="?android:attr/textAppearanceSmall"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/eth_con_type_dhcp"></RadioButton><RadioButton android:id="@+id/manual_radio"style="?android:attr/textAppearanceSmall"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/eth_con_type_manual"></RadioButton></RadioGroup><!-- IP address --><LinearLayout android:id="@+id/enterprise_wrapper"android:layout_width="fill_parent"android:layout_height="wrap_content"android:padding="0dip"android:orientation="vertical"><TextView android:id="@+id/ipaddr_text"style="?android:attr/textAppearanceSmall"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="8dip"android:text="@string/eth_ipaddr" /><EditText android:id="@+id/ipaddr_edit"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="2dip"android:singleLine="true" />
<!--<TextView android:id="@+id/netmask_text"style="?android:attr/textAppearanceSmall"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="8dip"android:text="@string/eth_mask" /><EditText android:id="@+id/netmask_edit"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="2dip"android:singleLine="true" />
--><TextView android:id="@+id/prefix_text"style="?android:attr/textAppearanceSmall"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="8dip"android:text="@string/eth_network_prefix_length" /><EditText android:id="@+id/prefix_edit"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="2dip"android:singleLine="true" /><TextView android:id="@+id/dns_text"style="?android:attr/textAppearanceSmall"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="8dip"android:text="@string/eth_dns" /><EditText android:id="@+id/eth_dns_edit"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="2dip"android:singleLine="true" /><TextView android:id="@+id/gw_text"style="?android:attr/textAppearanceSmall"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="8dip"android:text="@string/eth_gw" /><EditText android:id="@+id/eth_gw_edit"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="2dip"android:singleLine="true" /></LinearLayout></LinearLayout></ScrollView>

里面包括了dhcp与static ip的选择,以及static ip的设置

然后增加Settings/src/com/android/settings/ethernet/EthernetDialog.java
以及packages/apps/Settings/src/com/android/settings/ethernet/EthernetSettings.java
这些是具体的实现,最后我会给下载地址

最后出来的效果应该是这样的:

\

配制界面

\

增加这些修改和文件后,基本上就可以进行动态设置了。但是设置的关于ethernet的开关是没有作用的,所以这里增加开关的控制
修改文件frameworks/base/core/java/android/provider/Settings.java
在Settings数据库中增加一个ethernet的控制

public static final String ETHERNET_ON = "ethernet_on";//add by hclydao
增加这个以后需要更新api才能编译过,增加这个后在ethernet-service和Settings中ethernet中进行读写操作

修改frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetServiceImpl.java文件

在mHandler = new Handler(handlerThread.getLooper());后增加如下代码

int enable = Settings.Global.getInt(mContext.getContentResolver(),Settings.Global.ETHERNET_ON,0);//add by hclydao
if(enable != EthernetManager.ETH_STATE_ENABLED) {Log.i(TAG, "Ethernet is not enable");return;
}

如果没有打开就直接返回,不启动Service后继的操作,然后增加两个接口

	class TstartThread extends Thread {public void run() {Looper.prepare();mTracker.start(mContext, mHandler);mStarted.set(true);Looper.loop();}}public void Trackstart() { //add by hclydaonew TstartThread().start();}public void Trackstop() {Log.i(TAG, "Stop Ethernet service");Thread tstopthread = new Thread(new Runnable() {public void run() {Looper.prepare();mTracker.stop();mStarted.set(false);Looper.loop();}});tstopthread.start();}

同时要修改frameworks/base/core/java/android/net/EthernetManager.java文件增加

    public static final int ETH_STATE_UNKNOWN = 0;public static final int ETH_STATE_DISABLED = 1;public static final int ETH_STATE_ENABLED = 2;public void start() {try {mService.Trackstart();} catch (NullPointerException | RemoteException e) {}}public void stop() {try {mService.Trackstop();} catch (NullPointerException | RemoteException e) {}}


同时修改frameworks/base/core/java/android/net/IEthernetManager.aidl

增加

	void Trackstart();//add by hclydaovoid Trackstop();

提供给设置进行状态控制,这里基本功能就实现了。
跟踪测试时发现静态ip设置的时候有时候不成功,修改文件frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetNetworkFactory.java

增加

private Handler mHandler;
然后在

mContext = context;

后增加

mHandler = target;//add by hclydao

在if (!setStaticIpAddress(config.getStaticIpConfiguration())) {后增加

//if error then stop and restart add by hclydao
if((mContext != null) && (mHandler != null)) {Log.d(TAG, "Setting static ip failed now restart");stop();start(mContext,mHandler);
}

如果设置失败,stop后重新start

然后在if (mNMService.getinterfaceConfig(iface).hasFlag("running")) {前面增加

                        if(!iface.equals("eth0"))//add by hclydao make sure the interface is eth0continue;

这里只设置了一个设备名,为保存是eth0所以加上这句,防止意外.

最后来增加systemui statusbar中的状态提示,这里只增加了两种状态,一种是连接成功,一种是连接不成功,简单点来
修改frameworks/base/packages/SystemUI/res/layout/signal_cluster_view.xml

     <View
         android:id="@+id/wifi_signal_spacer"
         android:layout_width="4dp"
前增加

</FrameLayout><!--add by hclyado for ethernet--><FrameLayoutandroid:id="@+id/ethernet_combo"android:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_marginRight="-6dp"><ImageViewandroid:id="@+id/ethernet_state"android:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:scaleType="center"/></FrameLayout>

修改文件frameworks/base/packages/SystemUI/res/values/strings.xml
增加

    <!-- Content description of the Ethernet connected icon for accessibility (not shown on the screen). [CHAR LIMIT=NONE] add by hclydao--><string name="accessibility_ethernet_connected">Ethernet connected.</string><string name="accessibility_ethernet_disconnected">Ethernet disconnected.</string><string name="accessibility_ethernet_connecting">Ethernet connecting.</string>

修改文件
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java

增加

import android.net.EthernetManager;

在mWifiSignalController.notifyListeners();前面加上

cluster.setEthernetIndicators(false,R.drawable.ethernet_disconnected,R.string.accessibility_ethernet_disconnected);

在pushConnectivityToSignals函数中的
         mWifiSignalController.setInetCondition(
                 mValidatedTransports.get(mWifiSignalController.getTransportType()) ? 1 : 0);

后面增加

//add by hclydaoint length = mSignalClusters.size();int ethicon = R.drawable.ethernet_connecting;int ethacc = R.string.accessibility_ethernet_connecting;if(mValidatedTransports.get(TRANSPORT_ETHERNET)) {ethicon = R.drawable.ethernet_connected;ethacc = R.string.accessibility_ethernet_connected;}for (int i = 0; i < length; i++) {mSignalClusters.get(i).setEthernetIndicators(mEthernetConnected, ethicon,ethacc);}
//end add

void setIsAirplaneMode(boolean is, int airplaneIcon, int contentDescription);

后增加

public void setEthernetIndicators(boolean visible, int stateIcon, int contentDescription);

修改文件frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/SignalClusterView.java

增加

    private boolean mEthernetVisible = false;//add by hclydaoprivate int mEthernetStateId = 0;private int mEthernetDescription;ViewGroup mWifiGroup,mEthernetGroup;//modify by hclydaoImageView mVpn, mWifi, mAirplane, mNoSims,mEthernet;//modify by hclydao

         mWifiAirplaneSpacer =         findViewById(R.id.wifi_airplane_spacer);
         mWifiSignalSpacer =           findViewById(R.id.wifi_signal_spacer);
         mMobileSignalGroup = (LinearLayout) findViewById(R.id.mobile_signal_group);

后增加

        mEthernetGroup  = (ViewGroup) findViewById(R.id.ethernet_combo);//add by hclydaomEthernet       = (ImageView) findViewById(R.id.ethernet_state);

         mWifi           = null;
         mAirplane       = null;

后增加

        mEthernetGroup  = null;//add by hclydaomEthernet	    = null;

增加函数

	//add by hclydao@Overridepublic void setEthernetIndicators(boolean visible, int stateIcon, int contentDescription) {mEthernetVisible = visible;mEthernetStateId = stateIcon;mEthernetDescription = contentDescription;apply();}

在apply函数中
             mWifiSignalSpacer.setVisibility(View.GONE);
         }

后增加

        if (mEthernetVisible && !mWifiVisible) {//add by hclydaomEthernetGroup.setVisibility(View.VISIBLE);mEthernet.setImageResource(mEthernetStateId);mEthernetGroup.setContentDescription(mContext.getString(mEthernetDescription));} else {mEthernetGroup.setVisibility(View.GONE);}

在boolean anythingVisible = mNoSimsVisible || mWifiVisible || mIsAirplaneMode上增加

|| mEthernetVisible

最后效果图如下:

\


\


\


\

============================================
作者:hclydao
http://blog.csdn.net/hclydao
版权没有,但是转载请保留此段声明

============================================

参考 文章 :

http://blog.csdn.net/moyu123456789/article/details/50002099

http://my.oschina.net/hiliusl/blog/174973?fromerr=IPmtDOdk


相关源码下载地址:http://download.csdn.net/detail/hclydao/9472077


这篇关于android5.1 增加ethernet设置(DHCP与Static ip)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/717230

相关文章

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

Qt 设置软件版本信息的实现

《Qt设置软件版本信息的实现》本文介绍了Qt项目中设置版本信息的三种常用方法,包括.pro文件和version.rc配置、CMakeLists.txt与version.h.in结合,具有一定的参考... 目录在运行程序期间设置版本信息可以参考VS在 QT 中设置软件版本信息的几种方法方法一:通过 .pro

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

PostgreSQL 默认隔离级别的设置

《PostgreSQL默认隔离级别的设置》PostgreSQL的默认事务隔离级别是读已提交,这是其事务处理系统的基础行为模式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一 默认隔离级别概述1.1 默认设置1.2 各版本一致性二 读已提交的特性2.1 行为特征2.2

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

mtu设置多少网速最快? 路由器MTU设置最佳网速的技巧

《mtu设置多少网速最快?路由器MTU设置最佳网速的技巧》mtu设置多少网速最快?想要通过设置路由器mtu获得最佳网速,该怎么设置呢?下面我们就来看看路由器MTU设置最佳网速的技巧... 答:1500 MTU值指的是在网络传输中数据包的最大值,合理的设置MTU 值可以让网络更快!mtu设置可以优化不同的网

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

MySQL 设置AUTO_INCREMENT 无效的问题解决

《MySQL设置AUTO_INCREMENT无效的问题解决》本文主要介绍了MySQL设置AUTO_INCREMENT无效的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录快速设置mysql的auto_increment参数一、修改 AUTO_INCREMENT 的值。

详解Linux中常见环境变量的特点与设置

《详解Linux中常见环境变量的特点与设置》环境变量是操作系统和用户设置的一些动态键值对,为运行的程序提供配置信息,理解环境变量对于系统管理、软件开发都很重要,下面小编就为大家详细介绍一下吧... 目录前言一、环境变量的概念二、常见的环境变量三、环境变量特点及其相关指令3.1 环境变量的全局性3.2、环境变