如何开关APN网络

文章转自:http://www.javaeye.com/topic/565662

版权所有,转载请注明来自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 上联通和移动卡均是成功的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值