java - How to Set wifi proxy Hostname, port and bypass programetically in android 5.0 and above -
i want set wifi proxy hostname, port , bypass programmatically. follow link ->how can set proxysettings , proxyproperties on android wi-fi connection using java?. code not working on android 5.0.2. there other way set settings? in advance.
tested.. working in android 5.1.0, dont forget add permissions
wificonfiguration getcurrentwificonfiguration(wifimanager manager) { if (!manager.iswifienabled()) return null; list<wificonfiguration> configurationlist = manager.getconfigurednetworks(); wificonfiguration configuration = null; int cur = manager.getconnectioninfo().getnetworkid(); (int = 0; < configurationlist.size(); ++i) { wificonfiguration wificonfiguration = configurationlist.get(i); if (wificonfiguration.networkid == cur) configuration = wificonfiguration; } return configuration; } @targetapi(build.version_codes.lollipop) public void setwifiproxysettings5() { //get current wifi configuration wifimanager manager = (wifimanager)getsystemservice(context.wifi_service); wificonfiguration config = getcurrentwificonfiguration(manager); if(null == config) return; try { //linkproperties no longer in wificonfiguration class proxyinfoclass = class.forname("android.net.proxyinfo"); class[] sethttpproxyparams = new class[1]; sethttpproxyparams[0] = proxyinfoclass; class wificonfigclass = class.forname("android.net.wifi.wificonfiguration"); method sethttpproxy = wificonfigclass.getdeclaredmethod("sethttpproxy", sethttpproxyparams); sethttpproxy.setaccessible(true); //method 1 enum proxysettings in ipconfiguration class ipconfigclass = class.forname("android.net.ipconfiguration"); field f = ipconfigclass.getfield("proxysettings"); class proxysettingsclass = f.gettype(); //method 2 enum proxysettings in ipconfiguration //note $ between class , enum //class proxysettingsclass = class.forname("android.net.ipconfiguration$proxysettings"); class[] setproxysettingsparams = new class[1]; setproxysettingsparams[0] = proxysettingsclass; method setproxysettings = wificonfigclass.getdeclaredmethod("setproxysettings", setproxysettingsparams); setproxysettings.setaccessible(true); proxyinfo pi = proxyinfo.builddirectproxy("127.0.0.1", 8118); //android 5 supports pac file //enum value "pac" //proxyinfo pacinfo = proxyinfo.buildpacproxy(uri.parse("http://localhost/pac")); //pass new object sethttpproxy object[] params_sethttpproxy = new object[1]; params_sethttpproxy[0] = pi; sethttpproxy.invoke(config, params_sethttpproxy); //pass enum setproxysettings object[] params_setproxysettings = new object[1]; params_setproxysettings[0] = enum.valueof((class<enum>) proxysettingsclass, "static"); setproxysettings.invoke(config, params_setproxysettings); //save settings manager.updatenetwork(config); manager.disconnect(); manager.reconnect(); } catch(exception e) { log.v("wifiproxy", e.tostring()); } }
Comments
Post a Comment