android---APN切换

android手机客户端在上传文件时,有时候会一直失败,其可能的原因是APN的设置。wap下的成功率极低,所以在进行文件上传时最好设置下apn为net形式。下面是我在网上找的一些代码,是由wap转net的,当然net转wap稍微修改下就可以。经测试是可用的,分享一下:

PS:apn的切换过程需要时间,不是立即生效。

package com.android.couples;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class APNManager {
    private static String TAG = "APNManager";
    private static final Uri APN_TABLE_URI = Uri
            .parse("content://telephony/carriers");// 所有的APN配配置信息位置
    private static final Uri PREFERRED_APN_URI = Uri
            .parse("content://telephony/carriers/preferapn");// 当前的APN
    private static String[] projection = { "_id", "apn", "type", "current",
            "proxy", "port" };
    private static String APN_NET_ID = null;

    //切换成NETAPN
    public static boolean ChangeNetApn(final Context context) {
        final String wapId = getWapApnId(context);
        String apnId = getCurApnId(context);
        // 若当前apn是wap,则切换至net
        if (wapId.equals(apnId)) {
            APN_NET_ID = getNetApnId(context);
            setApn(context, APN_NET_ID);
            // 切换apn需要一定时间,先让等待几秒,与机子性能有关
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.d("xml", "setApn");
            return true;
        }
        return true;
    }

    //获取当前APN
    public static String getCurApnId(Context context) {
        ContentResolver resoler = context.getContentResolver();
        // String[] projection = new String[] { "_id" };
        Cursor cur = resoler.query(PREFERRED_APN_URI, projection, null, null,
                null);
        String apnId = null;
        if (cur != null && cur.moveToFirst()) {
            apnId = cur.getString(cur.getColumnIndex("_id"));
        }
        Log.i("xml","getCurApnId:"+apnId);
        return apnId;
    }

    public static APN getCurApnInfo(final Context context) {
        ContentResolver resoler = context.getContentResolver();
        // String[] projection = new String[] { "_id" };
        Cursor cur = resoler.query(PREFERRED_APN_URI, projection, null, null,
                null);
        APN apn = new APN();
        if (cur != null && cur.moveToFirst()) {
            apn.id = cur.getString(cur.getColumnIndex("_id"));
            apn.apn = cur.getString(cur.getColumnIndex("apn"));
            apn.type = cur.getString(cur.getColumnIndex("type"));

        }
        return apn;
    }

    
    public static void setApn(Context context, String id) {
        ContentResolver resolver = context.getContentResolver();
        ContentValues values = new ContentValues();
        values.put("apn_id", id);
        resolver.update(PREFERRED_APN_URI, values, null, null);
        Log.d("xml", "setApn");
    }

    //获取WAP APN
    public static String getWapApnId(Context context) {
        ContentResolver contentResolver = context.getContentResolver();
        // 查询cmwapAPN
        Cursor cur = contentResolver.query(APN_TABLE_URI, projection,
                "apn = \'cmwap\' and current = 1", null, null);
        // wap APN 端口不为空
        if (cur != null && cur.moveToFirst()) {
            do {
                String id = cur.getString(cur.getColumnIndex("_id"));
                String proxy = cur.getString(cur.getColumnIndex("proxy"));
                if (!TextUtils.isEmpty(proxy)) {
                	Log.i("xml","getWapApnId"+id);
                    return id;
                }
            } while (cur.moveToNext());
        }
        return null;
    }

    
    public static String getNetApnId(Context context) {
        ContentResolver contentResolver = context.getContentResolver();
        Cursor cur = contentResolver.query(APN_TABLE_URI, projection,
                "apn = \'cmnet\' and current = 1", null, null);
        if (cur != null && cur.moveToFirst()) {
            return cur.getString(cur.getColumnIndex("_id"));
        }
        return null;
    }

    //获取所有APN
       public static ArrayList<APN> getAPNList(final Context context) {

        ContentResolver contentResolver = context.getContentResolver();
        Cursor cr = contentResolver.query(APN_TABLE_URI, projection, null,
                null, null);

        ArrayList<APN> apnList = new ArrayList<APN>();

        if (cr != null && cr.moveToFirst()) {
            do{
                Log.d(TAG,
                        cr.getString(cr.getColumnIndex("_id")) + ";"
                                + cr.getString(cr.getColumnIndex("apn")) + ";"
                                + cr.getString(cr.getColumnIndex("type")) + ";"
                                + cr.getString(cr.getColumnIndex("current"))+ ";"
                                + cr.getString(cr.getColumnIndex("proxy")));
                APN apn = new APN();
                apn.id = cr.getString(cr.getColumnIndex("_id"));
                apn.apn = cr.getString(cr.getColumnIndex("apn"));
                apn.type = cr.getString(cr.getColumnIndex("type"));
                apnList.add(apn);
            }while(cr.moveToNext());
           
            cr.close();
        }
        return apnList;
    }

    //获取可用的APN
      public static ArrayList<APN> getAvailableAPNList(final Context context) {
        // current不为空表示可以使用的APN
        ContentResolver contentResolver = context.getContentResolver();
        Cursor cr = contentResolver.query(APN_TABLE_URI, projection,
                "current is not null" , null, null);
        ArrayList<APN> apnList = new ArrayList<APN>();
        if (cr != null && cr.moveToFirst()) {
            do{
                Log.d(TAG,
                        cr.getString(cr.getColumnIndex("_id")) + ";"
                                + cr.getString(cr.getColumnIndex("apn")) + ";"
                                + cr.getString(cr.getColumnIndex("type")) + ";"
                                + cr.getString(cr.getColumnIndex("current"))+ ";"
                                + cr.getString(cr.getColumnIndex("proxy")));
                APN apn = new APN();
                apn.id = cr.getString(cr.getColumnIndex("_id"));
                apn.apn = cr.getString(cr.getColumnIndex("apn"));
                apn.type = cr.getString(cr.getColumnIndex("type"));
                apnList.add(apn);
            }while (cr.moveToNext());
           
            cr.close();
        }
        return apnList;

    }
   //自定义APN包装类
    static class APN {

        String id;

        String apn;

        String type;

        public String toString() {
            return "id=" + id + ",apn=" + apn + ";type=" + type;
        }
    }
}


 


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
安卓调谐器Android Tuner:功能很强大的一个东西,只是大家使用的时候要注意,很多关键系统功能很有可能被误操作改掉了。   安卓调谐器说明:   安卓调谐器Android Tuner是一款应用合集,合并了Battery 显示器 Widget、System Tuner和Multi 切换 Widget的功能。   特有功能(需要root):   清理重新启动   开启/关闭 APN、 BT 发现、 GPS 和 WiFi 本地化   自动备份的已安装的应用程序   Logcat 阅读器为 Android 案例 4.1.x 的   图形监视窗口小部件   安卓调谐器特色: -支持Android 4.1.x Logcat 阅读器,自动备份的已安装的应用程序   - 可设置自动终止应用程序分类及触发条件,支持创建CWM备份包   - 清理缓存、垃圾、进程等后重新启动,4种图形监视窗口小部件   - 拥有功能超级强大的防火墙功能,管理每个访问网络应用程序   - 支持一键调整、调整ROM、内核、存储访问、内存管理器等   - 支持快速优化及清理,可设置备份更新、优化加载速度等   - 查看电池详细信息及健康状态,并进行估计和图标显示   - 可对系统控制进行调整、备份、还原默认、开机时应用   - 查看并管理开机启动项目、设置自定义程序开机自启   - 支持最大4*6面板的专家模式,便于打开想要的功能   - 开启/关闭 APN、 蓝牙发现、 GPS 和 WiFi本地化   - 可测试和调整SD卡读取速度,支持内存详细分析   更新日志:   Many compatibility improvements (external SD, various Android versions).   Improved network monitoring, Link2SD.   New CPUGPU controls for Galaxy S5 Exynos   Bug fixes and cosmetics

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值