2012-09-18 5 views
8

जावा (प्रोग्रामेटिक रूप से) का उपयोग कर एंड्रॉइड वाई-फाई कनेक्शन पर मैं प्रॉक्सी सेटिंग्स और प्रॉक्सीप्रॉपर्टी कैसे सेट कर सकता हूं?जावा का उपयोग कर एंड्रॉइड वाई-फाई कनेक्शन पर मैं प्रॉक्सी सेटिंग्स और प्रॉक्सीप्रॉपर्टी कैसे सेट कर सकता हूं?

रूप ipAssignment, linkProperties, ProxySettings और ProxyProperties एंड्रॉयड 3.1 पर WifiConfiguration भीतर क्षेत्रों छिपा हुआ है और ऊपर कर रहे हैं, मैं कक्षा Enum और फ़ील्ड का उपयोग करने में सक्षम होना चाहिए।

नीचे दिए गए लिंक का उपयोग कर, मैं एक विशेष वाई-फाई कनेक्शन के लिए एक स्थिर IP पता, प्रवेश द्वार और DNS सेट कर सकते हैं, लेकिन मैं यह भी Wificonfiguration.ProxySettings.STATIC निर्धारित करने की आवश्यकता कोड नमूना और ProxyProperties बाद

देखें स्टैक ओवरफ़्लो प्रश्न How to configue a static IP address, netmask, gateway programmatically on Android 3.x or 4.x

उदाहरण के लिए

,

WifiConfiguration config = new WifiConfiguration(configuration); 
config.ipAssignment = WifiConfiguration.IpAssignment.UNASSIGNED; 
config.proxySettings = WifiConfiguration.ProxySettings.STATIC; 
config.linkProperties.setHttpProxy(new ProxyProperties("127.0.0.1", 3128, "")); 

की तरह कुछ के लिए खोज रहे:

setProxySettings("STATIC", wifiConf); 
setProxyProperties("proxyserver.mine.com.au", 8080, ""); // Set Proxy server and port. 
wifiManager.updateNetwork(wifiConf); //apply the setting 

coolypf .ipAssignment .ProxySettings और linkProperties छिपे हुए हैं से निम्नलिखित कोड का उपयोग करना ...

WifiManager manager = (WifiManager)getSystemService(Context.WIFI_SERVICE); 
manager.asyncConnect(this, new Handler()); 
if (!manager.isWifiEnabled()) return; 
    List<WifiConfiguration> configurationList = manager.getConfiguredNetworks(); 
    WifiConfiguration configuration = null; 
    int cur = manager.getConnectionInfo().getNetworkId(); 
    for (int i = 0; i < configurationList.size(); ++i) 
    { 
     WifiConfiguration wifiConfiguration = configurationList.get(i); 
     if (wifiConfiguration.networkId == cur) 
     configuration = wifiConfiguration; 
    } 
    if (configuration == null) return; 
    WifiConfiguration config = new WifiConfiguration(configuration); 
    config.ipAssignment = WifiConfiguration.IpAssignment.UNASSIGNED; 
    config.proxySettings = WifiConfiguration.ProxySettings.STATIC; 

    config.linkProperties.clear(); 

    config.linkProperties.setHttpProxy(new ProxyProperties("127.0.0.1", 3128, "")); 

    manager.saveNetwork(config); 

उत्तर

12

यहां कुछ कोड दिया गया है जो आपको प्रॉक्सीप्रॉपर्टी सेट/सेट करने की अनुमति देनी चाहिए। यह उपरोक्त लिंक से कुछ कोड का उपयोग करता है। सेटिंग डिस्कनेक्ट/रीकनेक्ट के साथ प्रभावी नहीं लगती हैं।

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; 
} 

public static Object getDeclaredField(Object obj, String name) 
throws SecurityException, NoSuchFieldException, 
IllegalArgumentException, IllegalAccessException { 
    Field f = obj.getClass().getDeclaredField(name); 
    f.setAccessible(true); 
    Object out = f.get(obj); 
    return out; 
} 

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 void setProxySettings(String assign , WifiConfiguration wifiConf) 
throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException{ 
    setEnumField(wifiConf, assign, "proxySettings");  
} 


WifiConfiguration GetCurrentWifiConfiguration(WifiManager manager) 
{ 
    if (!manager.isWifiEnabled()) 
     return null; 

    List<WifiConfiguration> configurationList = manager.getConfiguredNetworks(); 
    WifiConfiguration configuration = null; 
    int cur = manager.getConnectionInfo().getNetworkId(); 
    for (int i = 0; i < configurationList.size(); ++i) 
    { 
     WifiConfiguration wifiConfiguration = configurationList.get(i); 
     if (wifiConfiguration.networkId == cur) 
      configuration = wifiConfiguration; 
    } 

    return configuration; 
} 

void setWifiProxySettings() 
{ 
    //get the current wifi configuration 
    WifiManager manager = (WifiManager)getSystemService(Context.WIFI_SERVICE); 
    WifiConfiguration config = GetCurrentWifiConfiguration(manager); 
    if(null == config) 
     return; 

    try 
    { 
     //get the link properties from the wifi configuration 
     Object linkProperties = getField(config, "linkProperties"); 
     if(null == linkProperties) 
      return; 

     //get the setHttpProxy method for LinkProperties 
     Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties"); 
     Class[] setHttpProxyParams = new Class[1]; 
     setHttpProxyParams[0] = proxyPropertiesClass; 
     Class lpClass = Class.forName("android.net.LinkProperties"); 
     Method setHttpProxy = lpClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams); 
     setHttpProxy.setAccessible(true); 

     //get ProxyProperties constructor 
     Class[] proxyPropertiesCtorParamTypes = new Class[3]; 
     proxyPropertiesCtorParamTypes[0] = String.class; 
     proxyPropertiesCtorParamTypes[1] = int.class; 
     proxyPropertiesCtorParamTypes[2] = String.class; 

     Constructor proxyPropertiesCtor = proxyPropertiesClass.getConstructor(proxyPropertiesCtorParamTypes); 

     //create the parameters for the constructor 
     Object[] proxyPropertiesCtorParams = new Object[3]; 
     proxyPropertiesCtorParams[0] = "127.0.0.1"; 
     proxyPropertiesCtorParams[1] = 8118; 
     proxyPropertiesCtorParams[2] = null; 

     //create a new object using the params 
     Object proxySettings = proxyPropertiesCtor.newInstance(proxyPropertiesCtorParams); 

     //pass the new object to setHttpProxy 
     Object[] params = new Object[1]; 
     params[0] = proxySettings; 
     setHttpProxy.invoke(linkProperties, params); 

     setProxySettings("STATIC", config); 

     //save the settings 
     manager.updateNetwork(config); 
     manager.disconnect(); 
     manager.reconnect(); 
    } 
    catch(Exception e) 
    { 
    } 
} 
void unsetWifiProxySettings() 
{ 
    WifiManager manager = (WifiManager)getSystemService(Context.WIFI_SERVICE); 
    WifiConfiguration config = GetCurrentWifiConfiguration(manager); 
    if(null == config) 
     return; 

    try 
    { 
     //get the link properties from the wifi configuration 
     Object linkProperties = getField(config, "linkProperties"); 
     if(null == linkProperties) 
      return; 

     //get the setHttpProxy method for LinkProperties 
     Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties"); 
     Class[] setHttpProxyParams = new Class[1]; 
     setHttpProxyParams[0] = proxyPropertiesClass; 
     Class lpClass = Class.forName("android.net.LinkProperties"); 
     Method setHttpProxy = lpClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams); 
     setHttpProxy.setAccessible(true); 

     //pass null as the proxy 
     Object[] params = new Object[1]; 
     params[0] = null; 
     setHttpProxy.invoke(linkProperties, params); 

     setProxySettings("NONE", config); 

     //save the config 
     manager.updateNetwork(config); 
     manager.disconnect(); 
     manager.reconnect(); 
    } 
    catch(Exception e) 
    { 
    } 
} 
+0

एक बड़ा आप कार्ल धन्यवाद! :) – user1681648

+0

आपका क्या मतलब है "सेटिंग डिस्कनेक्ट/रीकनेक्ट के साथ प्रभावी नहीं लगती हैं।" ? – David

+1

यह एंड्रॉइड 2.3.6 पर काम नहीं कर रहा है। त्रुटि: "ऐसा कोई क्षेत्र अपवाद नहीं है" रेखा: ऑब्जेक्ट लिंकप्रॉपर्टीज = getField (config, "linkProperties"); setWifiProxySettings() –

4

ऊपर दिए गए कार्ल के उत्तर के समान प्रारूप के बाद एंड्रॉइड 5.0 में एक ही चीज़ को संभालने के लिए कुछ नमूना कोड यहां दिया गया है।

public void setWifiProxySettings5() 
{ 
    //get the current wifi configuration 
    WifiManager manager = (WifiManager)getSystemService(Context.WIFI_SERVICE); 
    WifiConfiguration config = GetCurrentWifiConfiguration(manager); 
    if(null == config) 
     return;  

    try 
    {  
     //linkProperties is 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 to get the ENUM ProxySettings in IpConfiguration 
     Class ipConfigClass = Class.forName("android.net.IpConfiguration"); 
     Field f = ipConfigClass.getField("proxySettings"); 
     Class proxySettingsClass = f.getType();   

     //Method 2 to get the ENUM ProxySettings in IpConfiguration 
     //Note the $ between the class and 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 a PAC file 
     //ENUM value is "PAC" 
     //ProxyInfo pacInfo = ProxyInfo.buildPacProxy(Uri.parse("http://localhost/pac")); 

     //pass the new object to setHttpProxy 
     Object[] params_SetHttpProxy = new Object[1]; 
     params_SetHttpProxy[0] = pi; 
     setHttpProxy.invoke(config, params_SetHttpProxy); 

     //pass the enum to setProxySettings 
     Object[] params_setProxySettings = new Object[1]; 
     params_setProxySettings[0] = Enum.valueOf((Class<Enum>) proxySettingsClass, "STATIC"); 
     setProxySettings.invoke(config, params_setProxySettings); 

     //save the settings 
     manager.updateNetwork(config); 
     manager.disconnect(); 
     manager.reconnect(); 
    } 
    catch(Exception e) 
    { 
     Log.v("wifiProxy", e.toString()); 
    } 
} 
+1

एंड्रॉइड 6 के लिए यह कोड भी काम करता है, लेकिन दुर्भाग्यवश आप उस नेटवर्क को अपडेट नहीं कर सकते जिसे आपने स्वयं नहीं बनाया है। यह नए एंड्रॉइड वाईफ़ाई नियमों के कारण है: http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-network – radu122

3

डेव के जवाब की तरह, लेकिन कम लाइनों एकमात्र तरीका setProxy(ProxySettings settings, ProxyInfo proxy) (आसपास के स्पष्टता के लिए छोड़े गए कोड) का उपयोग करके:

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); 

ProxyInfo desiredProxy = ProxyInfo.buildDirectProxy(YOUR_HOST, YOUR_PORT); 

Object[] methodParams = new Object[2]; 
methodParams[0] = Enum.valueOf(proxySettings, "STATIC"); 
methodParams[1] = desiredProxy; 

setProxy.invoke(config, methodParams); 
संबंधित मुद्दे

 संबंधित मुद्दे