android 没有对于APN网络的API没有公开,但是我们可以更新数据库,系统会自动监听数据库的变化,从而实现开启/关闭APN。

源代码中frameworks/base/core/java/android/provider/Telephony.java这个类,介绍了相关的URI和数据库字段:content://telephony/carriers

字段可以在Telephony.java中找到。

打开APN ,可以设置正确的APN参数;

关闭APN,可以设置错误的参数。



首先判断移动网络是否连接:

ConnectivityManager connectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);if(connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == State.CONNECTED){  true;}else{  false;}


打开/关闭:

        /** * 打开移动网络,通过设置一个正确的APN参数 * @param context */public static void openApn(Context context){ArrayList<APNInfos> apnList = new ArrayList<APNInfos>();apnList = getAPNList(context);for(APNInfos apnInfos : apnList){log("-openApn--getApn-----" + apnInfos.getApn());}for(APNInfos apnInfos : apnList){ContentValues values = new ContentValues(); values.put("apn",APNMatchTools.matchAPN(apnInfos.getApn()));values.put("type",APNMatchTools.matchAPN(apnInfos.getType()));context.getContentResolver().update(Contents.apnUri, values, "_id=?", new String[]{apnInfos.getId()});}}/** * 关闭移动网络,通过设置一个错误的APN参数 * @param context */public static void closeApn(Context context){ArrayList<APNInfos> apnList = new ArrayList<APNInfos>();apnList = getAPNList(context);for(APNInfos apnInfos : apnList){log("--closeApn-getApn-----" + apnInfos.getApn());}for(APNInfos apnInfos : apnList){ContentValues values = new ContentValues(); values.put("apn",APNMatchTools.matchAPN(apnInfos.getApn()) + "mdev");values.put("type",APNMatchTools.matchAPN(apnInfos.getType()) + "mdev");context.getContentResolver().update(Contents.apnUri, values, "_id=?", new String[]{apnInfos.getId()});}}

获取APN列表:

        /** * 获取APN列表 * @param context * @return */public static ArrayList<APNInfos> getAPNList(Context context){ArrayList<APNInfos> apnList = new ArrayList<APNInfos>();String[] projection = {"_id,apn,type,current"}; Cursor cr = context.getContentResolver().query(Contents.apnUri, projection, null, null, null);while(cr !=null && cr.moveToNext()){ if(cr.getString(cr.getColumnIndex("apn")).equals("")){}else{APNInfos apnInfos = new APNInfos();apnInfos.setId(cr.getString(cr.getColumnIndex("_id")));apnInfos.setApn(cr.getString(cr.getColumnIndex("apn")));apnInfos.setType(cr.getString(cr.getColumnIndex("type")));apnList.add(apnInfos);}}if(cr != null){cr.close();}return apnList;}


APNMatchTools类:

public class APNMatchTools {public static String matchAPN(String currentName){if(currentName == null || currentName.equals("")){return "";}currentName = currentName.toLowerCase();//变成小写字母if(currentName.startsWith(Contents.CMNET)){return Contents.CMNET;}else if(currentName.startsWith(Contents.CMWAP)){return Contents.CMWAP;}else if(currentName.startsWith(Contents.GNET_3)){return Contents.GNET_3;}else if(currentName.startsWith(Contents.GWAP_3)){return Contents.GWAP_3;}else if(currentName.startsWith(Contents.UNINET)){return Contents.UNINET;}else if(currentName.startsWith(Contents.UNIWAP)){return Contents.UNIWAP;}else if(currentName.startsWith("default")){return "default";}else{return "";}}}

其中 几个变量

        public static final String CMWAP = "cmwap";   //中国移动cmwap public static final String CMNET = "cmnet";   //中国移动cmnet public static final String GWAP_3 = "3gwap";  //中国联通3gwappublic static final String GNET_3 = "3gnet";  //中国联通3gnetpublic static final String UNIWAP = "uniwap"; //中国联通uni wappublic static final String UNINET = "uninet"; //中国联通uni net


APNInfos类

public class APNInfos {private String id, apn ,type;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getApn() {return apn;}public void setApn(String apn) {this.apn = apn;}public String getType() {return type;}public void setType(String type) {this.type = type;}}



更多相关文章

  1. androidhttp网络通信
  2. Android之访问网络,使用HttpURLConnection还是HttpClient?
  3. Android 网络(一) HTTP协议
  4. Android网络请求心路历程
  5. [导入]2010-03-03 传智播客—Android(五)数据存储之五网络数据交互
  6. Android 与 后台网络交互踩坑
  7. Android dumpsys 使用之分析网络使用情况

随机推荐

  1. Android动态壁纸开发
  2. Android(安卓)开发笔记 - Android(安卓)S
  3. Android弹窗
  4. Android(安卓)SDK,API版本对照
  5. Android:简单联网获取网页代码
  6. 组件居中显示 安卓
  7. Android开发问题记录——“The connectio
  8. 第一行代码笔记之——Activity启动模式
  9. Android图文布局【整理】
  10. Android(安卓)开发笔记 - Android(安卓)S