android中設定代理程式碼
世界美景發表於2017-11-03
記錄下android中設定代理程式碼 或許有朋友能用的上
適用於4.4.3 在5.0上android.net.ProxyProperties 找不到 估計API被谷歌拿掉了 4.4.4還沒試估計API還在
private static String NOTPROXY = ""; //不走代理名單
private static List<String> list;
//type為1設定wifi設定 為0是清除代理
public static void setWifi(Context context, final String ip, final int port, final int type) throws SecurityException, IllegalArgumentException,
NoSuchFieldException, IllegalAccessException,
NoSuchMethodException, ClassNotFoundException,
InstantiationException, InvocationTargetException {
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final String ssid = wifiManager.getConnectionInfo().getSSID();
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
changeNotProxy();
for (WifiConfiguration config : configuredNetworks) {
Log.d("txy", "current SSID is" + config.SSID);
if (config.SSID.toString().contains(Common.MYSSID)) { //需要給哪個無線設定代理
Class proxyPropertiesClass = Class
.forName("android.net.ProxyProperties");
Constructor c = proxyPropertiesClass.getConstructor(String.class,
Integer.TYPE, String.class);
c.setAccessible(true);
//wifiIP 為代理的IP地址 如:192.168.0.111 wifiPort為代理埠 如888
setProxy(config, (type == 0 ? null : c.newInstance(ip, port, NOTPROXY)), type);
wifiManager.updateNetwork(config);
break;
} else {
// wifiManager.removeNetwork(config.networkId);
}
}
wifiManager.disconnect();
wifiManager.reconnect();
}
public static void setObjField(Object obj, Object value, String name)
throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
Field f = obj.getClass().getDeclaredField(name);
f.setAccessible(true);
f.set(obj, value);
}
//type為1設定wifi設定 為0是清除代理
public static WifiConfiguration setProxy(WifiConfiguration wifiConf, Object properties, int type)
throws SecurityException, IllegalArgumentException,
NoSuchFieldException, IllegalAccessException,
NoSuchMethodException, ClassNotFoundException,
InstantiationException, InvocationTargetException {
if (type == 0) setEnumField(wifiConf, "NONE", "proxySettings");
else setEnumField(wifiConf, "STATIC", "proxySettings");
Object linkProperties = getField(wifiConf, "linkProperties");
setObjField(linkProperties, properties, "mHttpProxy");
setObjField(wifiConf, linkProperties, "linkProperties");
return wifiConf;
}
public static void setEnumField(Object obj, String value, String name)
throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
Field f = obj.getClass().getField(name);
f.set(obj, Enum.valueOf((Class<Enum>) f.getType(), value));
}
public static Object getField(Object obj, String name)
throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
Field f = obj.getClass().getField(name);
Object out = f.get(obj);
return out;
}
5.0
private static List<String> list;//不走代理名單
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static void set_proxy(Application context, String ip, String port, int type) throws Throwable
{
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wi = wifiManager.getConnectionInfo();
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
changeNotProxy();
for(WifiConfiguration config : configuredNetworks)
{
//需要給哪個無線設定代理
if(config.SSID.equals(wi.getSSID()))
{
Class proxySettings = Class.forName("android.net.IpConfiguration$ProxySettings");
Class[] setProxyParams = new Class[2];
setProxyParams[0] = proxySettings;
setProxyParams[1] = ProxyInfo.class;
Method setProxy = config.getClass().getDeclaredMethod("setProxy", setProxyParams);
setProxy.setAccessible(true);
if(type == 0)
{
Object[] methodParams = new Object[2];
methodParams[0] = Enum.valueOf(proxySettings, "NONE");
methodParams[1] = null;
setProxy.invoke(config, methodParams);
}
else
{
ProxyInfo desiredProxy = ProxyInfo.buildDirectProxy(ip, Integer.parseInt(port),list);
Object[] methodParams = new Object[2];
methodParams[0] = Enum.valueOf(proxySettings, "STATIC");
methodParams[1] = desiredProxy;
setProxy.invoke(config, methodParams);
}
//save the settings
wifiManager.updateNetwork(config);
wifiManager.disconnect();
wifiManager.reconnect();
break;
}
}
}
相關文章
- android WIFI 設定代理程式碼 4.4.3——5.02017-11-03AndroidWiFi
- git 設定代理下載 webrtc程式碼2018-06-27GitWebC程式
- Git中設定代理和取消代理2018-02-01Git
- android-studio設定代理2016-11-25Android
- Android 桌布設定程式碼 詳解2014-07-15Android
- java程式設定http,socks代理2015-08-03JavaHTTP
- Android SDK可用伺服器代理設定2017-11-23Android伺服器
- HttpClientBuilder中設定代理伺服器 - TCoolsIT2020-04-03HTTPclientUI伺服器
- Nginx 代理設定2017-11-17Nginx
- pnpm 設定代理2024-03-19NPM
- 代理的設定2024-07-12
- docker 設定代理2024-08-14Docker
- Android Eclipse程式碼自動補全設定2014-07-09AndroidEclipse
- 怎麼實現代理伺服器設定隧道代理IP的詳細步驟(程式碼)2023-10-12伺服器
- 設計模式(七)Android中的代理模式2019-03-19設計模式Android
- 直播系統程式碼,Android獲取、設定鎖屏密碼2021-12-22Android密碼
- npm 設定埠代理2020-07-23NPM
- nginx 反向代理設定2017-11-22Nginx
- Gradle 代理設定2015-08-30Gradle
- sdk更新代理設定2016-06-16
- anaconda 設定代理2024-04-25
- linux 設定代理2024-07-20Linux
- 直播小程式原始碼,Android 傾斜文字 效果設定2023-10-19原始碼Android
- Android判斷當前裝置是否設定了代理2018-03-13Android
- 在程式碼中設定IE9的預設文件模式2013-08-16IE9模式
- Mac怎麼設定代理IP,蘋果系統設定http代理ip教程2022-06-11Mac蘋果HTTP
- 分享一段Android許可權設定的程式碼2020-11-23Android
- jQuery設定select選中項程式碼例項2017-04-14jQuery
- MacOS設定終端代理2021-12-26Mac
- Nginx、haproxy反向代理設定2017-04-01Nginx
- 為終端設定代理2016-12-19
- tomcat代理的設定2017-09-03Tomcat
- git 設定和取消代理2017-07-26Git
- C# WebBrowser設定代理2013-09-11C#Web
- ubuntu 給 apt 設定代理2024-06-30UbuntuAPT
- vite 設定網路代理2024-10-24Vite
- 程式碼設定RelativeLayout相對位置設定2017-11-17
- Android中怎樣設定文字居中2014-12-22Android