android5.1 增加ethernet设置(DHCP与Static 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-tile
		    android: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 network 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="com.android.settings.ethernet.EthernetSettings" />
            <meta-data android:name="com.android.settings.TOP_LEVEL_HEADER_ID"
                android:resource="@id/ethernet_settings" />
            <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
                android:value="true" />
        </activity>

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

 

内容如下

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project

     Licensed 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 at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     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 and
     limitations 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">
        <Preference
            android: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">

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="8dip"
            android:orientation="vertical">

            <LinearLayout
                android: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 hclydao
		new 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 hclydao
	void 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 eth0
                            continue;

 

这里只设置了一个设备名,为保存是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-->
	<FrameLayout
	    android:id="@+id/ethernet_combo"
	    android:layout_height="wrap_content"
	    android:layout_width="wrap_content"
	    android:layout_marginRight="-6dp"
	    >
	    <ImageView
	        android: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 hclydao
        int 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 hclydao
    private int mEthernetStateId = 0;
	private int mEthernetDescription;
    ViewGroup mWifiGroup,mEthernetGroup;//modify by hclydao
    ImageView 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 hclydao
        mEthernet       = (ImageView) findViewById(R.id.ethernet_state);

         mWifi           = null;
         mAirplane       = null;

 

后增加

        mEthernetGroup  = null;//add by hclydao
        mEthernet	    = null;

增加函数

	//add by hclydao
	@Override
    public 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 hclydao
            mEthernetGroup.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

下载文件为zip文件,上传后自动在最后加了_ 下载后将_删除

有积分的麻烦支持下使用积分下载

没有积分的,想办法给点积分然后去github下载

CSDN升级排版就不改了

地址:  https://github.com/hcly/android5.1.1-ethernet

 

 

  • 4
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 46
    评论
### 回答1: Android 5.1是一个广泛使用的操作系统版本,支持HDMI输出功能。要设置HDMI分辨率,可以按照以下步骤进行: 1. 首先,将Android设备与HDMI显示器或电视连接。确保HDMI线缆正常连接。 2. 在Android设备上打开设置菜单。可以通过下拉通知栏或点击主屏幕上的设置图标来访问设置菜单。 3. 在设置菜单中,向下滚动并找到“显示”选项。点击进入显示设置页面。 4. 在显示设置页面中,可能会看到“屏幕投射”或“显示模式”的选项。点击进入相关设置。 5. 在屏幕投射或显示模式设置页面中,可以看到HDMI选项。点击进入HDMI设置。 6. 在HDMI设置页面中,通常会有分辨率选项。点击进入分辨率设置。 7. 在分辨率设置页面中,可以看到可用的分辨率选项列表。根据显示器或电视的支持能力和个人偏好,选择所需的分辨率。 8. 选择完分辨率后,点击确认或应用。系统将应用所选的分辨率设置。 9. 返回到上一个菜单或主屏幕,查看是否已成功设置HDMI分辨率。显示器或电视上的画面应该会根据所选的分辨率进行调整。 需要注意的是,不同的Android设备或系统版本可能会有略微不同的设置流程。因此,根据具体的设备和系统版本,上述步骤可能会有所变化。但总体而言,通过进入设置菜单,找到显示设置,再进入HDMI设置,选择适当的分辨率,就可以完成Android 5.1设置HDMI分辨率的流程。 ### 回答2: Android 5.1版本的设备在设置HDMI分辨率时,可以按照以下步骤进行操作: 1. 连接HDMI线缆:首先,将一端的HDMI线缆插入Android设备的HDMI输出接口,另一端插入显示设备(如电视或投影仪)的HDMI输入接口。 2. 打开设置界面:在Android设备上,找到并点击打开“设置”应用程序,通常可以在应用程序列表中找到该选项。 3. 进入显示设置:在“设置”主界面中,向下滚动并找到“显示”选项,点击进入显示设置界面。 4. 选择HDMI设置:在显示设置界面中,找到并点击“HDMI”选项,这将打开HDMI设置界面。 5. 选择分辨率:在HDMI设置界面中,通常会显示可用的HDMI分辨率选项。根据你的显示设备和个人需求,选择适当的分辨率选项。 6. 保存设置:选择完分辨率后,点击界面上的“保存”或“应用”按钮,以保存并应用新的HDMI分辨率设置。 7. 测试分辨率:你可以通过在显示设备上观察图像是否清晰和完整来测试新的HDMI分辨率设置。如果满意,设置流程就结束了。 请注意,以上步骤仅适用于Android 5.1版本的系统设备,不同的Android版本或设备类型可能略有不同。确保你的设备支持HDMI输出功能并运行在Android 5.1版本或更高版本。 ### 回答3: 要设置Android 5.1的HDMI分辨率,可以按照以下流程操作: 1. 首先,确保你的Android设备已连接到HDMI显示器上。 2. 在设备上滑动屏幕,进入主菜单,找到并点击“设置”图标。 3. 在设置菜单中,向下滚动找到“显示”或类似的选项,并点击进入。 4. 在显示设置菜单中,找到“屏幕分辨率”或类似的选项,并点击进入。 5. 系统会列出可用的屏幕分辨率选项。根据你的需求和HDMI显示器的能力,选择一个适合的分辨率。 6. 点击选中分辨率后,系统会提示你是否确认应用此分辨率。确认后,系统会应用新的分辨率设置。 7. 返回到主菜单或桌面,你会注意到HDMI显示器的分辨率已经改变为你所设置的分辨率。 需要注意的是,不同的Android设备可能会在设置菜单的布局和选项名称上有所区别,具体操作可能会有所不同。但一般来说,通过“设置”-“显示”-“屏幕分辨率”可以找到相关的选项。 如果你在Android设备上找不到上述选项,可能是因为该设备的固件版本或制造商定制的界面导致了菜单布局的差异。在这种情况下,你可能需要查阅设备的用户手册或进行在线搜索来了解具体的设置方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值