步骤1:在frameworks/base/packages/SettingsProvider/res/values/defaults.xml 中增加一行:<bool name="def_Ethernet_share">true</bool>,默认开关打开
步骤2:在frameworks/base/core/java/android/provider/Settings.java 中增加开关命名:public static final String Enable_Ethernet_share = "Ethernet_share";
步骤3:在frameworks/base/core/java/android/provider/Settings.java 中添加对应开关名:
public static final String[] SETTINGS_TO_BACKUP = {
ACCESSIBILITY_SPEAK_PASSWORD,
++Enable_Ethernet_share,
步骤4:在frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java中增加设置初始化开关值。以上默认打开:
loadBooleanSetting(stmt, Settings.Secure.Enable_Ethernet_share, R.bool.def_Ethernet_share);
步骤5:在packages/apps/Settings/res/xml/accessibility_settings.xml
<SwitchPreference
android:key="Ethernet_share_preference"
android:title="Ethernet_share"
android:persistent="false"/>
步骤6:在packages/apps/Settings/src/com/android/settings/accessibility中修改AccessibilitySettings.java文件
public class AccessibilitySettings extends SettingsPreferenceFragment implements DialogCreatable,
Preference.OnPreferenceChangeListener, Indexable {
++private static final String "Ethernet_share_PREFERENCE ="Ethernet_share_preference";
++private SwitchPreference mEthernetsharePreference;
在private void initializeAllPreferences()中添加
mEthernetsharePreference=
(SwitchPreference) findPreference(Ethernet_share_PREFERENCE );
在private void updateSystemPreferences添加
final boolean EthernetShareEnabled = Settings.Secure.getInt(getContentResolver(),
Settings.Secure.Enable_Ethernet_share, 0) != 0;
mEthernetsharePreference.setChecked(EthernetShareEnabled);
在public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) 添加
else if (mEthernetsharePreference == preference) {
handleEthernetsharePreferenceClick();
return true;
}
然后添加方法:
private void handleEthernetsharePreferenceClick() {
Settings.Secure.putInt(getContentResolver(),
Settings.Secure.Enable_Ethernet_share,
mEthernetsharePreference.isChecked() ? 1 : 0);
if(mEthernetsharePreference.isChecked()){
boolean success = EthernetShareUtil.doEthernetShare(EthernetShareUtil.getDns());
}
}
这样控制开关就完成了,在设置--无障碍选项当中,接下来实现EthernetShareUtil.doEthernetShare这个功能就好,我是以下方法做的
在packages/apps/Settings/src/com/android/settings/video这个位置新建一个java文件,命名为EthernetShareUtil.java,里面的内容就是实现以太网共享4G网络的,大致方法就是配置 系统的路由策略,有不懂的可以咨询我。
public static boolean doEthernetShare(String dns) {
if(TextUtils.isEmpty(dns)) return false;
DataOutputStream dataOutputStream = null;
BufferedReader errorStream = null;
try {
Process process = Runtime.getRuntime().exec("su");
dataOutputStream = new DataOutputStream(process.getOutputStream());
String commandStr = "mount -o rw,remount / " + " \n" +
"mount -o rw,remount /system " + " \n" +
"mkdir -p /var/lib/misc/" + " \n" +
"touch /var/lib/misc/udhcpd.leases" + " \n" +
"echo 0 > /proc/sys/net/ipv4/ip_forward" + " \n" +
"busybox ifconfig eth0 192.168.1.123 netmask 255.255.255.0" + " \n" +
"ndc tether interface add eth0" + " \n" +
"ndc tether start 192.168.1.2 192.168.1.254" + " \n" +
"ip rule add from all lookup main pref 9999" + " \n" +
"ndc nat enable eth0 ppp0 2 192.168.1.133/32" + " \n" +
"echo 1 > /proc/sys/net/ipv4/ip_forward" + " \n" +
"iptables -t nat -I PREROUTING -i eth0 -p udp --dport 53 -j DNAT --to-destination "+dns + " \n" ;
dataOutputStream.write(commandStr.getBytes(Charset.forName("utf-8")));
dataOutputStream.flush();
dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
process.waitFor();
errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String msg = "";
String line;
// 读取命令的执行结果
while ((line = errorStream.readLine()) != null) {
msg += line;
}
System.out.println(msg);
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (dataOutputStream != null) {
dataOutputStream.close();
}
if (errorStream != null) {
errorStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
该方法需要在开机启动的时候执行一次,可以在开机广播的地方添加判断开关是否有开启,若有开启,则执行一次。这地方还遇到个问题,系统起来后功能方法已经执行了,但是4G网络还没有起来,所以我这边处理方法是将系统启动延时大概5S,这时候确保4G网络起来后,通过方法就可以将4G网络共享出来了
以上讲述的实现过程,都是在framework和上层实现的,不需要底层部分支持