Settings.Secure.HTTP_PROXY deprecated in ICS but n

2019-03-25 04:20发布

问题:

The Android developer docs say the following about Settings.Secure.HTTP_PROXY:

Host name and port for global http proxy. Uses ':' seperator for between host and port TODO - deprecate in favor of global_http_proxy_host, etc

But there is not any information about global_http_proxy_host. Now I always get null when trying to read Settings.Secure.HTTP_PROXY.

Settings.Secure.HTTP_PROXY was working properly in all releases before ICS.

回答1:

You can use Java Reflection to set the global proxy tested on ICS.

UPDATED CODE

Activity activity = this;


private void setProxtAndPortOnICS(String porxyServer2, int port2)
{
            try
{
  Class jwcjb = Class.forName("android.webkit.JWebCoreJavaBridge");
  Class params[] = new Class[1];
  params[0] = Class.forName("android.net.ProxyProperties");
  Method updateProxyInstance = jwcjb.getDeclaredMethod("updateProxy", params);

  Class wv = Class.forName("android.webkit.WebView");
  Field mWebViewCoreField = wv.getDeclaredField("mWebViewCore");
  Object mWebViewCoreFieldIntance = getFieldValueSafely(mWebViewCoreField, oauthPage);

  Class wvc = Class.forName("android.webkit.WebViewCore");
  Field mBrowserFrameField = wvc.getDeclaredField("mBrowserFrame");
  Object mBrowserFrame = getFieldValueSafely(mBrowserFrameField, mWebViewCoreFieldIntance);

  Class bf = Class.forName("android.webkit.BrowserFrame");
  Field sJavaBridgeField = bf.getDeclaredField("sJavaBridge");
  Object sJavaBridge = getFieldValueSafely(sJavaBridgeField, mBrowserFrame);

  Class ppclass = Class.forName("android.net.ProxyProperties");
 Class pparams[] = new Class[3];
 pparams[0] = String.class;
 pparams[1] = int.class;
 pparams[2] = String.class;
 Constructor ppcont = ppclass.getConstructor(pparams);

 updateProxyInstance.invoke(sJavaBridge, ppcont.newInstance("my.proxy.com", 1234, null)); 
}
catch (Exception ex)
{    
 }


 }


 private Object getFieldValueSafely(Field field, Object classInstance) throws IllegalArgumentException, IllegalAccessException {
   boolean oldAccessibleValue = field.isAccessible();
   field.setAccessible(true);
   Object result = field.get(classInstance);
   field.setAccessible(oldAccessibleValue);
   return result;      
}

NOW you can filter out the urls using proxy server.

OR look at this blog this is in Chinese but you can read the code it is fairly easy to understand.



回答2:

I'm just going by what the documentation says, but it reads to me that Settings.Secure.HTTP_PROXY isn't currently deprecated. The (sloppy) note in the documentation was just a developer's note that this is something the Android team may consider doing in the future.