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. ...
随机推荐
- ,net运行框架
.NET FrameWork框架 是一套应用程序开发框架,主要目的提供一个开发模型. 主要的两个组件: 公共语言运行时(Common Language Runtime)(CLR): 提供内存管理.线程 ...
- 浅谈HTTP中Get、Post、Put与Delete的区别
Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而HTTP ...
- Windows 下目录及文件向Linux同步
本文解决的是Windows 下目录及文件向Linux同步的问题,Windows向 Windows同步的请参考:http://www.idcfree.com/article-852-1.html 环境介 ...
- ORACLE参数文件
一.oracle pfile/spfile区别 pfile 默认的名称为“init+例程名.ora”文件路径:E:\oracle\product\10.2.0\db_1\dbs,这是一个文本文件 ...
- oracle 导入导出数据
Oracle数据导入导出imp/exp就相当于oracle数据还原与备份.exp命令可以把数据从远程数据库服务器导出到本地的dmp文件,imp命令可以把dmp文件从本地导入到远处的数据库服务器中.利用 ...
- 安装ejabberd2并配置MySQL为其数据库
以前用过openfire做为服务器,但是openfire的集群支持不是很好,所以改用Ejabberd,由于它是用Erlang语言开发的,其并发率与分布式的功能都是很强悍的,在此我记录一下我的安装与配置 ...
- system CPU占用率过高与91助手的关系
今天正在认真工作,忽然发现电脑越来越慢. 按 CTRL+ALT+DEL打开任务管理器看了下CPU使用率.其中system占用率居然达到了64%.不对劲儿,按照平时习惯,system根本占用了不要这么多 ...
- Entity Framework Code First级联删除
如果我们要到一对主从表增加级联删除,则要在主表中的引用属性上增加Required关键字,如: public class Destination { public int DestinationId { ...
- 如何在网页中显示pdf
用如下的html代码即可(例子): <div class="postBody"> <div id="cnblogs_post_body"> ...
- MongoDB之二基础入门(安装启动)
mongodb中有三元素:数据库,集合,文档,其中“集合” 就是对应关系数据库中的“表”,“文档”对应“行”. 一. 下载 上MongoDB官网 ,我们发现有32bit和64bit,这个就要看你系统了 ...