版权所有,转载请注明来自Mobile Developer (http://mdev.cc) 作者 : SinFrancis



由于Android对于APN的网络API没有公开,不过我们可以阅读源代码,然后进行数据库操作,系统会自动监听数据库的变化,从而实现开启或者关闭APN。

大家可以研究一下frameworks/base/core/java/android/provider/Telephony.java这个类,

比较重要的就是 URI 和数据库字段:content://telephony/carriers

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

其实原理很简单 :

1 、 当开启APN的时候,设置一个正确的移动或者联通的APN

2、 关闭的时候设置一个错误APN就会自动关闭网络

请看代码:Activity:

Java代码
  1. packagecc.mdev.apn;
  2. importjava.util.ArrayList;
  3. importjava.util.List;
  4. importandroid.app.Activity;
  5. importandroid.content.ContentValues;
  6. importandroid.database.Cursor;
  7. importandroid.net.Uri;
  8. importandroid.os.Bundle;
  9. importandroid.util.Log;
  10. importandroid.view.View;
  11. importandroid.widget.Button;
  12. /**
  13. *這裡是Activity
  14. *@authorSinFranciswong
  15. *@sitehttp://mdev.cc
  16. *@wikihttp://mdev.cc/wiki
  17. *@since2010-01-08
  18. */
  19. publicclassMainextendsActivity{
  20. /**Calledwhentheactivityisfirstcreated.*/
  21. Uriuri=Uri.parse("content://telephony/carriers");
  22. @Override
  23. publicvoidonCreate(BundlesavedInstanceState){
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.main);
  26. Buttonopen=(Button)findViewById(R.id.open);
  27. Buttonclose=(Button)findViewById(R.id.close);
  28. open.setOnClickListener(newView.OnClickListener(){
  29. @Override
  30. publicvoidonClick(Viewv){
  31. openAPN();
  32. }
  33. });
  34. close.setOnClickListener(newView.OnClickListener(){
  35. @Override
  36. publicvoidonClick(Viewv){
  37. closeAPN();
  38. }
  39. });
  40. }
  41. publicvoidopenAPN(){
  42. List<APN>list=getAPNList();
  43. for(APNapn:list){
  44. ContentValuescv=newContentValues();
  45. cv.put("apn",APNMatchTools.matchAPN(apn.apn));
  46. cv.put("type",APNMatchTools.matchAPN(apn.type));
  47. getContentResolver().update(uri,cv,"_id=?",newString[]{apn.id});
  48. }
  49. }
  50. publicvoidcloseAPN(){
  51. List<APN>list=getAPNList();
  52. for(APNapn:list){
  53. ContentValuescv=newContentValues();
  54. cv.put("apn",APNMatchTools.matchAPN(apn.apn)+"mdev");
  55. cv.put("type",APNMatchTools.matchAPN(apn.type)+"mdev");
  56. getContentResolver().update(uri,cv,"_id=?",newString[]{apn.id});
  57. }
  58. }
  59. privateList<APN>getAPNList(){
  60. Stringtag="Main.getAPNList()";
  61. //current不为空表示可以使用的APN
  62. Stringprojection[]={"_id,apn,type,current"};
  63. Cursorcr=this.getContentResolver().query(uri,projection,null,null,null);
  64. List<APN>list=newArrayList<APN>();
  65. while(cr!=null&&cr.moveToNext()){
  66. Log.d(tag,cr.getString(cr.getColumnIndex("_id"))+""+cr.getString(cr.getColumnIndex("apn"))+""+cr.getString(cr.getColumnIndex("type"))+""+cr.getString(cr.getColumnIndex("current")));
  67. APNa=newAPN();
  68. a.id=cr.getString(cr.getColumnIndex("_id"));
  69. a.apn=cr.getString(cr.getColumnIndex("apn"));
  70. a.type=cr.getString(cr.getColumnIndex("type"));
  71. list.add(a);
  72. }
  73. if(cr!=null)
  74. cr.close();
  75. returnlist;
  76. }
  77. publicstaticclassAPN{
  78. Stringid;
  79. Stringapn;
  80. Stringtype;
  81. }
  82. }

APNMatchTools.java

Java代码
  1. packagecc.mdev.apn;
  2. /**
  3. *這裡是APN匹配,用於匹配移動或者聯通的APN
  4. *@authorSinFranciswong
  5. *@sitehttp://mdev.cc
  6. *@wikihttp://mdev.cc/wiki
  7. *@since2010-01-08
  8. *
  9. */
  10. publicfinalclassAPNMatchTools{
  11. publicstaticclassAPNNet{
  12. /**
  13. *中国移动cmwap
  14. */
  15. publicstaticStringCMWAP="cmwap";
  16. /**
  17. *中国移动cmnet
  18. */
  19. publicstaticStringCMNET="cmnet";
  20. //中国联通3GWAP设置中国联通3G因特网设置中国联通WAP设置中国联通因特网设置
  21. //3gwap3gnetuniwapuninet
  22. /**
  23. *3Gwap中国联通3gwapAPN
  24. */
  25. publicstaticStringGWAP_3="3gwap";
  26. /**
  27. *3Gnet中国联通3gnetAPN
  28. */
  29. publicstaticStringGNET_3="3gnet";
  30. /**
  31. *uniwap中国联通uniwapAPN
  32. */
  33. publicstaticStringUNIWAP="uniwap";
  34. /**
  35. *uninet中国联通uninetAPN
  36. */
  37. publicstaticStringUNINET="uninet";
  38. }
  39. publicstaticStringmatchAPN(StringcurrentName){
  40. if("".equals(currentName)||null==currentName){
  41. return"";
  42. }
  43. currentName=currentName.toLowerCase();
  44. if(currentName.startsWith(APNNet.CMNET))
  45. returnAPNNet.CMNET;
  46. elseif(currentName.startsWith(APNNet.CMWAP))
  47. returnAPNNet.CMWAP;
  48. elseif(currentName.startsWith(APNNet.GNET_3))
  49. returnAPNNet.GNET_3;
  50. elseif(currentName.startsWith(APNNet.GWAP_3))
  51. returnAPNNet.GWAP_3;
  52. elseif(currentName.startsWith(APNNet.UNINET))
  53. returnAPNNet.UNINET;
  54. elseif(currentName.startsWith(APNNet.UNIWAP))
  55. returnAPNNet.UNIWAP;
  56. elseif(currentName.startsWith("default"))
  57. return"default";
  58. elsereturn"";
  59. //returncurrentName.substring(0,currentName.length()-SUFFIX.length());
  60. }
  61. }

最后不要忘记加上修改APN的权限:

Xml代码
  1. <uses-permissionandroid:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>

经过测试在G1 上联通和移动卡均是成功的。


更多相关文章

  1. Android(安卓)-- ViewRoot,关于子线程刷新UI
  2. listview为空时,显示字符串
  3. Android(安卓)listview中嵌套Checkbox的布局文件
  4. Android之ListView属性描述
  5. kotlin初窥之Kotlin Android(安卓)Extensions
  6. Android的源代码结构
  7. Android应用程序启动过程源代码分析
  8. 安卓开发学习之012 TextView高级应用
  9. Android知识点记录: 使用代码设置 android 上listView的条目的点

随机推荐

  1. 求助:从库mysqldump 时,mysql 有时会导致重
  2. mysql锁研究系列一(锁的基本概念)
  3. 数据库截取字符串SUBSTR函数的使用
  4. linux安装Jdk、Tomcat、Nginx、Mysql简介
  5. ubuntu16.04 mysql5.7.20表中插入中文显
  6. PHPnow 升级后 PHP不支持GD、MySQL
  7. Mysql5.7.10版本安装后空密码登录,退出后
  8. spark学习-SparkSQL--07-SparkContext类
  9. 如何通过使用where子句与字符串格式(varch
  10. 关于mysql的sql_mode的问题