Getting WiFi proxy settings in Android

前端 未结 4 1120
天涯浪人
天涯浪人 2020-12-08 06:00

I am trying to read WIFI proxy settings

  • Proxy host
  • Proxy port
  • Proxy user (authentication)
  • Proxy password (authentication)
4条回答
  •  萌比男神i
    2020-12-08 06:10

    This is what I'm using:

    public static String[] getUserProxy(Context context)
    {
        Method method = null;
        try
        {
          method = ConnectivityManager.class.getMethod("getProxy");
        }
        catch (NoSuchMethodException e)
        {
          // Normal situation for pre-ICS devices
          return null;
        }
        catch (Exception e)
        {
          return null;
        }
    
        try
        {
          ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          Object pp = method.invoke(connectivityManager);
          if (pp == null)
            return null;
    
          return getUserProxy(pp);
        }
        catch (Exception e)
        {
          return null;
        }
      }
    
    
    private static String[] getUserProxy(Object pp) throws Exception
    {
        String[] userProxy = new String[3];
    
        String className = "android.net.ProxyProperties";
        Class c = Class.forName(className);
        Method method;
    
        method = c.getMethod("getHost");
        userProxy[0] = (String) method.invoke(pp);
    
        method = c.getMethod("getPort");
        userProxy[1] = String.valueOf((Integer) method.invoke(pp));
    
    
        method = c.getMethod("getExclusionList");
        userProxy[2] = (String) method.invoke(pp);
    
        if (userProxy[0] != null)
          return userProxy;
        else
          return null;
    }
    

提交回复
热议问题