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;
- }
- }
- }
android---APN切换的更多相关文章
- android 语言切换过程分析
android 语言切换过程分析 2014-02-27 18:13 1207人阅读 评论(0) 收藏 举报 语言切换android语言切换android改变语言 最近在看一个bug,系统切换语言后,本 ...
- cocos2dx shader实现灰度图android后台切换回来导致图像偏移的问题
转自:http://www.tuicool.com/articles/U3URRrI 项目中经常会遇到将一张图像处理成灰色的需求,为了节省资源,一般不会让美术再做一套同样的灰度图,通常会通过代码处理让 ...
- android Button 切换背景,实现动态按钮和按钮颜色渐变
android Button 切换背景,实现动态按钮和按钮颜色渐变 一.添加android 背景筛选器selector实现按钮背景改变 1.右键单击项目->new->Oth ...
- Android Listview切换动画,扩展到任意view切换之间动画实现
添加布局如下: <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2010 ...
- Android技术——切换视图(两)随着ViewPage达到Tab幻灯片浏览
Android技术--切换视图(一)~(四)在资源项目:https://github.com/YongYuIT/MeiNv_Liulanqi 一.早期android(android.support.v ...
- [FMX]将 Android 程序切换到后台及从后台切换到前台实现
有时候,我们需要将自己的Android程序切换到后台运行,在必要时,将其切换到前台运行.下面提供了一种实现方式,首先需要引用三个单元: 1 uses Androidapi.JNI.App,Andr ...
- Android Activity 切换动画(非原创)
在Android开发过程中,经常会碰到Activity之间的切换效果的问题,下面介绍一下如何实现左右滑动的切换效果,首先了解一下Activity切换的实现,从Android2.0开始在Activity ...
- Android 横屏切换竖屏Activity的生命周期(转)
曾经遇到过一个面试题,让你写出横屏切换竖屏Activity的生命周期.现在给大家分析一下他切换时具体的生命周期是怎么样的: 1.新建一个Activity,并把各个生命周期打印出来 2.运行Acti ...
- Android开发切换host应用
由于在工作过程中常需要切换手机的host来测试不同服务器上的接口,所以想到需要这么个软件. SwitchHost在PC上是一款很好用的修改Host的软件,手机上也需要这么一款App(当然手机需要已经R ...
- 【转】Android 语言切换过程分析
最近在看一个bug,系统切换语言后,本来退到后台的音乐,会在通知栏上显示通知.为了解决这个bug,我学习了下android的语言切换流程,也参考了大量其他人的资料.(主要参考了http://blog. ...
随机推荐
- knockout 绑定 jquery ui datepicker (转)
ko.bindingHandlers.datepicker = { init: function(element, valueAccessor, allBindingsAccessor) { //in ...
- CPU占用率高分析方法步骤[转载]
由于涉及到私有代码,所有图片都隐去 1.执行TOP命令,确认CPU占用较高的进程PID 根据top命令,发现PID为8691的Java进程占用CPU高达3858%,出现故障 2.确认该进程中CPU占用 ...
- webstorm使用教程--主题设置
1. 代码字体设置 :注意需要另存为 Scheme name一个才可以修改Editor Font. 字体设置:Editor->Color & Fonts->Font 2. 界面字体 ...
- (转载)memcpy的几个实现版本
(转载)http://blog.sina.com.cn/s/blog_4d3a41f40100cvza.html 实现void *memcpy(void *to, const void *from, ...
- android数据库(随apk一起发布数据库)
读取数据库+数据库版本更新 注意: a, 将随apk发布的数据库放在android工程下/res/raw路径下. b, 数据库文件存到手机上时,路径在/data/data/你的包名/databases ...
- 单元测试工具之Xunit
在.NET开发中的单元测试工具之——xUnit.Net 原始出处 http://zhoufoxcn.blog.51cto.com/792419/1172320 在上一篇<在.NET开发中的单元 ...
- 第一章:绪论-Python开发工具的安装
书中提到了操作系统平台尽量选 *nix.我这里选用的是 ubuntu 14.04 , 下面的操作均以此操作系统为例说明. 操作系统安装教程可以去网站上找,推荐用虚拟机的方式,Windows下可用的虚拟 ...
- STM32使用以下规则对过滤器编号:
STM32使用以下规则对过滤器编号:(1) FIFO_0和 FIFO_1的过滤器分别独立编号,均从0开始按顺序编号.(2) 所有关联同一个 FIFO 的过滤器,不管有没有被激活,均统一进行编号.(3) ...
- algorithm: heap sort in python 算法导论 堆排序
An Python implementation of heap-sort based on the detailed algorithm description in Introduction to ...
- 【转载】linux命令行计算器bc的一个“坑”
[转载自]http://blog.chinaunix.net/uid-174325-id-3518953.html 结论:ibase,obase可以使用在不同的计算公式里,但是尽量把obase放iba ...