安卓11添加切换以太网动态静态方法

news/2024/5/17 15:35:06 标签: android, gitee

客户要在app中自由切换动态,静态方法,直接把系统jar-api给他搞了半天搞不定,只有在系统里给他实现一个接口,方法如下:

Index: packages/apps/Settings/AndroidManifest.xml
===================================================================
--- packages/apps/Settings/AndroidManifest.xml	(revision 868)
+++ packages/apps/Settings/AndroidManifest.xml	(revision 869)
@@ -5,7 +5,7 @@
         android:sharedUserId="android.uid.system">
 
     <original-package android:name="com.android.settings" />
-
+     <uses-permission android:name="android.net.action.ETHERNET_IP_CHANGED"/>
     <uses-permission android:name="android.permission.REQUEST_NETWORK_SCORES" />
     <uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
     <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
@@ -3504,6 +3504,14 @@
             </intent-filter>/>
         </receiver>
 
+	<receiver android:name=".ethernet.EtherentBoardcastReceiver"
+			 android:enabled="true"
+			 android:exported="true">
+             <intent-filter android:priority="1000">
+                <action android:name="android.net.action.ETHERNET_IP_CHANGED" />
+            </intent-filter>
+        </receiver>
+
         <!-- This is the longest AndroidManifest.xml ever. -->
     </application>
 </manifest>
Index: packages/apps/Settings/src/com/android/settings/ethernet/EtherentBoardcastReceive.java
===================================================================
--- packages/apps/Settings/src/com/android/settings/ethernet/EtherentBoardcastReceive.java     (nonexistent)
+++ packages/apps/Settings/src/com/android/settings/ethernet/EtherentBoardcastReceive.java     (revision 869)
@@ -0,0 +1,220 @@
+package com.android.settings.ethernet;
+
+import com.android.settings.R;
+import com.android.settings.SettingsPreferenceFragment;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.app.admin.DevicePolicyManager;
+import android.content.ActivityNotFoundException;
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.PackageManager;
+import android.content.res.Resources;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
+import android.os.SystemProperties;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.preference.CheckBoxPreference;
+import android.preference.Preference.OnPreferenceChangeListener;
+import android.preference.PreferenceScreen;
+import android.provider.SearchIndexableResource;
+import android.provider.Settings;
+import android.telephony.TelephonyManager;
+import android.text.TextUtils;
+import android.util.Log;
+import android.content.Intent;
+
+import androidx.preference.SwitchPreference;
+import androidx.preference.ListPreference;
+import androidx.preference.Preference;
+
+import java.io.File;
+import java.io.FileDescriptor;
+import java.io.File;
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+import java.util.regex.Pattern;
+import java.lang.Integer;
+import java.net.InetAddress;
+import java.net.Inet4Address;
+import java.util.Iterator;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+import android.preference.Preference.OnPreferenceChangeListener;
+import android.preference.Preference.OnPreferenceClickListener;
+
+import com.android.settings.SettingsPreferenceFragment.SettingsDialogFragment;
+
+
+/*for 5.0*/
+import android.net.EthernetManager;
+import android.net.IpConfiguration;
+import android.net.IpConfiguration.IpAssignment;
+import android.net.IpConfiguration.ProxySettings;
+import android.net.wifi.SupplicantState;
+import android.net.wifi.WifiInfo;
+import android.net.wifi.WifiManager;
+import android.net.StaticIpConfiguration;
+import android.net.NetworkUtils;
+import android.net.LinkAddress;
+import android.net.LinkProperties;
+import android.widget.Toast;
+//import android.preference.ListPreference;
+//import com.android.internal.logging.MetricsProto.MetricsEvent;
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+
+
+import com.android.settings.ethernet.ethernet_static_ip_dialog;
+public class EtherentBoardcastReceiver extends BroadcastReceiver  {
+	private static final String TAG = "EtherentBoardcastReceiver";
+	 
+	private  static String mEthMode = null
+    private  static String mEthIpAddress = null;
+    private  static String mEthNetmask = null;
+    private  static String mEthGateway = null;
+    private  static String mEthdns1 = null;
+    private  static String mEthdns2 = null;
+
+    private final static String nullIpInfo = "0.0.0.0";
+	private String mIfaceName;
+	 @Override
+    public void onReceive(Context context, Intent intent) {
+
+			IpConfiguration mIpConfiguration;
+			StaticIpConfiguration mStaticIpConfiguration;
+			String action = intent.getAction();
+		    Log.i(TAG, "receive a new action : " + action);
+			
+			EthernetManager mEthManager = (EthernetManager) context.getSystemService(Context.ETHERNET_SERVICE);
+			if(mEthManager == null){
+				Log.i(TAG, "fail to getSystemService :  "+Context.ETHERNET_SERVICE);
+			}
+
+		  mStaticIpConfiguration =new StaticIpConfiguration();
+		  getNetConfigFromIntent(intent);
+		 /*
+		  * get ip address, netmask,dns ,gw etc.
+		  */
+		String[] ifaces = mEthManager.getAvailableInterfaces();
+        if (ifaces.length > 0) {
+            mIfaceName = ifaces[0];//"eth0";
+        }
+        if (null == mIfaceName) {
+            Log.e(TAG, "get ethernet ifaceName failed");
+            
+        }
+
+
+		if (mEthMode  == null ||mEthMode.equals("static"))
+		{
+			 Inet4Address inetAddr = getIPv4Address(this.mEthIpAddress);
+			int prefixLength = maskStr2InetMask(this.mEthNetmask); 
+			InetAddress gatewayAddr = getIPv4Address(this.mEthGateway); 
+			InetAddress dnsAddr1 = getIPv4Address(this.mEthdns1);
+			InetAddress dnsAddr2 = getIPv4Address(this.mEthdns2);
+			if(inetAddr ==null || gatewayAddr == null || prefixLength <= 0  ){
+				  Log.e(TAG,"ip,mask or dnsAddr is wrong");
+				  return ;
+			}
+			if (inetAddr.getAddress().toString().isEmpty() || prefixLength ==0 || gatewayAddr.toString().isEmpty()
+			  ) {
+				   Log.e(TAG,"ip,mask or dnsAddr is wrong");
+				  return ;
+			}
+			  
+			mStaticIpConfiguration.ipAddress = new LinkAddress(inetAddr, prefixLength);
+			mStaticIpConfiguration.gateway=gatewayAddr;
+			if(dnsAddr1 != null && !dnsAddr1.toString().isEmpty())
+				mStaticIpConfiguration.dnsServers.add(dnsAddr1);
+			if(dnsAddr2 != null && !dnsAddr2.toString().isEmpty())
+				mStaticIpConfiguration.dnsServers.add(dnsAddr2);
+		
+			mIpConfiguration=new IpConfiguration(IpAssignment.STATIC, ProxySettings.NONE,mStaticIpConfiguration,null);  
+			mEthManager.setConfiguration(mIfaceName, mIpConfiguration); 
+		}else{
+			mEthManager.setConfiguration(mIfaceName, new IpConfiguration(IpAssignment.DHCP, ProxySettings.NONE,null,null));
+		}
+       
+		//
+		
+    }
+	
+	private void getNetConfigFromIntent(Intent intent){
+		Bundle bundle = intent.getExtras();
+        if (bundle.getString("netMode") != null)
+			this.mEthMode = bundle.getString("netMode");
+		if (bundle.getString("ipaddr") != null)
+			this.mEthIpAddress = bundle.getString("ipaddr");
+		if (bundle.getString("netMask")!= null)
+			this.mEthNetmask = bundle.getString("netMask");
+		if (bundle.getString("gateway")!= null)
+			this.mEthGateway = bundle.getString("gateway");
+		if (bundle.getString("dns1") != null)
+			this.mEthdns1 = bundle.getString("dns1");
+		if (bundle.getString("dns2") != null)
+			this.mEthdns2 = bundle.getString("dns2");
+	}
+	 private Inet4Address getIPv4Address(String text) {
+        try {
+            return (Inet4Address) NetworkUtils.numericToInetAddress(text);
+        } catch (IllegalArgumentException|ClassCastException e) {
+            return null;
+        }
+    }
+
+	 /*
+     * convert subMask string to prefix length
+     */
+    private int maskStr2InetMask(String maskStr) {
+    	StringBuffer sb ;
+    	String str;
+    	int inetmask = 0; 
+    	int count = 0;
+    	/*
+    	 * check the subMask format
+    	 */
+      	Pattern pattern = Pattern.compile("(^((\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])$)|^(\\d|[1-2]\\d|3[0-2])$");
+    	if (pattern.matcher(maskStr).matches() == false) {
+    		Log.e(TAG,"subMask is error");
+    		return 0;
+    	}
+    	
+    	String[] ipSegment = maskStr.split("\\.");
+    	for(int n =0; n<ipSegment.length;n++) {
+    		sb = new StringBuffer(Integer.toBinaryString(Integer.parseInt(ipSegment[n])));
+    		str = sb.reverse().toString();
+    		count=0;
+    		for(int i=0; i<str.length();i++) {
+    			i=str.indexOf("1",i);
+    			if(i==-1)  
+    				break;
+    			count++;
+    		}
+    		inetmask+=count;
+    	}
+    	return inetmask;
+    }
+
+
+
+}
+

http://www.niftyadmin.cn/n/5263028.html

相关文章

lwIP 细节之六:connected、sent、poll 回调函数是何时调用的

使用 lwIP 协议栈进行 TCP 裸机编程&#xff0c;其本质就是编写协议栈指定的各种回调函数。将你的应用逻辑封装成函数&#xff0c;注册到协议栈&#xff0c;在适当的时候&#xff0c;由协议栈自动调用&#xff0c;所以称为回调。 注&#xff1a;除非特别说明&#xff0c;以下内…

论文阅读《Parameterized Cost Volume for Stereo Matching》

论文地址&#xff1a;https://openaccess.thecvf.com/content/ICCV2023/papers/Zeng_Parameterized_Cost_Volume_for_Stereo_Matching_ICCV_2023_paper.pdf 源码地址&#xff1a;https://github.com/jiaxiZeng/Parameterized-Cost-Volume-for-Stereo-Matching 概述 现有的立体匹…

前端怎么调用node接口---小白

1.基于node搭建express后端脚手架&#xff1a;基于node搭建express后端脚手架 2.在node里边写一个接口 // 引入express const express require(express) // 创建实例 const app express() // 创建监听端口 const port 3000 // 定义接口 app.get(/api/getData,(req,res) &g…

单通道led线性驱动芯片推荐:SM2082EGS

单通道LED线性驱动芯片是一种用于控制LED灯的芯片&#xff0c;它能够提供恒定的电流输出&#xff0c;从而实现LED灯的稳定亮度调节。这种芯片主要由输入端、控制电路、放大器和输出端构成&#xff0c;通过控制输入端的电压和信号来调节LED的亮度。 单通道led线性驱动芯片推荐&a…

WebSocket 协议的原理和实践

WebSocket 是一种基于 TCP 的双向通信协议&#xff0c;它可以在客户端和服务器之间建立一个持久连接&#xff0c;并且可以在连接生命周期内进行数据的传输。WebSocket 协议的目的是为了解决 HTTP 协议的一些局限性&#xff0c;比如&#xff1a; HTTP 协议是单向的&#xff1a;…

HPV为什么无症状?皮肤性病科专家谭巍解读具体原因

HPV&#xff0c;即人乳头瘤病毒&#xff0c;是一种常见的性传播疾病。然而&#xff0c;并不是所有感染HPV的人都会出现症状。为什么有的人感染HPV没有症状呢? 首先&#xff0c;需要了解的是&#xff0c;HPV感染是一种非常常见的现象。事实上&#xff0c;大约有80%的性活跃人群…

Keepalived+Nginx实现高可用(下)

一、背景 上篇文章介绍了基本的Keepalived的简单入门&#xff0c;但是针对预留的问题还有优化的空间。分别是下面3个问题: 1、如果仅仅只提供一个VIP的方式&#xff0c;会存在只有1台服务器处于实际工作&#xff0c;另外1台处于闲置状态。 势必存在成本资源浪费问题&#xff0c…

SQL Server查询计划(Query Plan)——图形查询计划

6.4. 查询计划 与Oracle等其他关系库类似,SQL Server中,查询计划是进行SQL调优的基础,没有拿到SQL语句的查询计划之前,任何人都没办法对其性能方面进行准确的分析和判断,也没办法对相关性能问题进行精准的定位(当然,该过程中,也需要参考SQL语句及其具体数据环境)。所…