Set Proxy for Android 4.4 and above. - Android Network

Android examples for Network:Proxy

Description

Set Proxy for Android 4.4 and above.

Demo Code

/*//from   w w  w. j a v a  2s  . c o m
 * Copyright (C) 2013 Daniel Velazco
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;
import android.content.Context;
import android.content.Intent;
import android.net.Proxy;

import android.os.Parcelable;
import android.util.ArrayMap;
import android.util.Log;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    private static final String LOG_TAG = "WebViewProxyUtil";

    /**
     * Set Proxy for Android 4.4 and above.
     */
    @SuppressWarnings("all")
    private static boolean setKitKatWebViewProxy(Context appContext,
            String host, int port) {
        System.setProperty("http.proxyHost", host);
        System.setProperty("http.proxyPort", port + "");
        System.setProperty("https.proxyHost", host);
        System.setProperty("https.proxyPort", port + "");
        try {
            Class applictionCls = Class.forName("android.app.Application");
            Field loadedApkField = applictionCls
                    .getDeclaredField("mLoadedApk");
            loadedApkField.setAccessible(true);
            Object loadedApk = loadedApkField.get(appContext);
            Class loadedApkCls = Class.forName("android.app.LoadedApk");
            Field receiversField = loadedApkCls
                    .getDeclaredField("mReceivers");
            receiversField.setAccessible(true);
            ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
            for (Object receiverMap : receivers.values()) {
                for (Object rec : ((ArrayMap) receiverMap).keySet()) {
                    Class clazz = rec.getClass();
                    if (clazz.getName().contains("ProxyChangeListener")) {
                        Method onReceiveMethod = clazz.getDeclaredMethod(
                                "onReceive", Context.class, Intent.class);
                        Intent intent = new Intent(
                                Proxy.PROXY_CHANGE_ACTION);

                        /*********** optional, may be need in future *************/
                        final String CLASS_NAME = "android.net.ProxyProperties";
                        Class cls = Class.forName(CLASS_NAME);
                        Constructor constructor = cls.getConstructor(
                                String.class, Integer.TYPE, String.class);
                        constructor.setAccessible(true);
                        Object proxyProperties = constructor.newInstance(
                                host, port, null);
                        intent.putExtra("proxy",
                                (Parcelable) proxyProperties);
                        /*********** optional, may be need in future *************/

                        onReceiveMethod.invoke(rec, appContext, intent);
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            Log.e(LOG_TAG,
                    "Setting proxy with >= 4.4 API failed with error: "
                            + e.getMessage());
            return false;
        } catch (NoSuchFieldException e) {
            Log.e(LOG_TAG,
                    "Setting proxy with >= 4.4 API failed with error: "
                            + e.getMessage());
            return false;
        } catch (IllegalAccessException e) {
            Log.e(LOG_TAG,
                    "Setting proxy with >= 4.4 API failed with error: "
                            + e.getMessage());
            return false;
        } catch (IllegalArgumentException e) {
            Log.e(LOG_TAG,
                    "Setting proxy with >= 4.4 API failed with error: "
                            + e.getMessage());
            return false;
        } catch (NoSuchMethodException e) {
            Log.e(LOG_TAG,
                    "Setting proxy with >= 4.4 API failed with error: "
                            + e.getMessage());
            return false;
        } catch (InvocationTargetException e) {
            Log.e(LOG_TAG,
                    "Setting proxy with >= 4.4 API failed with error: "
                            + e.getMessage());
            return false;
        } catch (InstantiationException e) {
            Log.e(LOG_TAG,
                    "Setting proxy with >= 4.4 API failed with error: "
                            + e.getMessage());
            return false;
        }

        Log.d(LOG_TAG, "Setting proxy with >= 4.4 API successful!");
        return true;
    }
}

Related Tutorials