---
title: "Android对于APN"
author: "helpbs"
date: "2011-12-27T14:53:00Z"
tags: []
slug: "android-apn"
draft: false
lastmod: "2012-04-24T08:51:08Z"
id: 1495945
url: "https://helpbs.iteye.com/blog/1495945"
---

版权所有，转载请注明来自Mobile Developer ([http://mdev.cc](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：

[view plain](http://blog.csdn.net/chenlaic/article/details/6086844 "view plain")

01. packagecc.mdev.apn;
02. importjava.util.ArrayList;
03. importjava.util.List;
04. importandroid.app.Activity;
05. importandroid.content.ContentValues;
06. importandroid.database.Cursor;
07. importandroid.net.Uri;
08. importandroid.os.Bundle;
09. 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&lt;APN&gt;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&lt;APN&gt;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&lt;APN&gt;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&lt;APN&gt;list=newArrayList&lt;APN&gt;();
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

[view plain](http://blog.csdn.net/chenlaic/article/details/6086844 "view plain")

01. packagecc.mdev.apn;
02. /\**
03. \*這裡是APN匹配，用於匹配移動或者聯通的APN
04. \*@authorSinFranciswong
05. \*@sitehttp://mdev.cc
06. \*@wikihttp://mdev.cc/wiki
07. \*@since2010-01-08
08. \*
09. \*/
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代码[](http://blog.csdn.net/chenlaic/article/details/6086844 "复制代码")

1. **&lt;uses-permission**android:name="android.permission.WRITE\_APN\_SETTINGS"**&gt;&lt;/uses-permission&gt;**

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