get Network Apn Proxy - Android Network

Android examples for Network:Network Status

Description

get Network Apn Proxy

Demo Code


//package com.java2s;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;

public class Main {
    public static Proxy getApnProxy(Context context) {


        NetworkInfo ni = ((ConnectivityManager) context
                .getApplicationContext().getSystemService("connectivity"))
                .getActiveNetworkInfo();

        if (ni.isConnected()) {
            return null;
        }// w ww.jav a  2 s. c o m

        Cursor c = getCurrentApn(context);
        if (c.getCount() > 0) {
            c.moveToFirst();
            long id = c.getLong(c.getColumnIndex("_id"));

            String proxy = c.getString(c.getColumnIndex("proxy"));
            String port = c.getString(c.getColumnIndex("port"));

            c.close();

            if ((proxy != null) && (!proxy.equals(""))) {
                if (validateIp(proxy)) {
                    return new Proxy(Proxy.Type.HTTP,
                            InetSocketAddress.createUnresolved(proxy,
                                    Integer.valueOf(port).intValue()));
                }

                return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
                        proxy, Integer.valueOf(port).intValue()));
            }
        }

        c.close();

        return null;
    }

    public static Cursor getCurrentApn(Context context) {
        return context.getContentResolver().query(
                Uri.parse("content://telephony/carriers/preferapn"), null,
                null, null, null);
    }

    public static boolean validateIp(String aIp) {
        String IPADDRESS_PATTERN = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

        Pattern pattern = Pattern.compile(IPADDRESS_PATTERN);
        Matcher matcher = pattern.matcher(aIp);
        return matcher.matches();
    }
}

Related Tutorials