教你如何开关Android的APN网络
16lz
2021-12-04
版权所有,转载请注明来自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代码
- packagecc.mdev.apn;
- importjava.util.ArrayList;
- importjava.util.List;
- importandroid.app.Activity;
- importandroid.content.ContentValues;
- importandroid.database.Cursor;
- importandroid.net.Uri;
- importandroid.os.Bundle;
- importandroid.util.Log;
- importandroid.view.View;
- importandroid.widget.Button;
- /**
- *這裡是Activity
- *@authorSinFranciswong
- *@sitehttp://mdev.cc
- *@wikihttp://mdev.cc/wiki
- *@since2010-01-08
- */
- publicclassMainextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- Uriuri=Uri.parse("content://telephony/carriers");
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Buttonopen=(Button)findViewById(R.id.open);
- Buttonclose=(Button)findViewById(R.id.close);
- open.setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- openAPN();
- }
- });
- close.setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- closeAPN();
- }
- });
- }
- publicvoidopenAPN(){
- List<APN>list=getAPNList();
- for(APNapn:list){
- ContentValuescv=newContentValues();
- cv.put("apn",APNMatchTools.matchAPN(apn.apn));
- cv.put("type",APNMatchTools.matchAPN(apn.type));
- getContentResolver().update(uri,cv,"_id=?",newString[]{apn.id});
- }
- }
- publicvoidcloseAPN(){
- List<APN>list=getAPNList();
- for(APNapn:list){
- ContentValuescv=newContentValues();
- cv.put("apn",APNMatchTools.matchAPN(apn.apn)+"mdev");
- cv.put("type",APNMatchTools.matchAPN(apn.type)+"mdev");
- getContentResolver().update(uri,cv,"_id=?",newString[]{apn.id});
- }
- }
- privateList<APN>getAPNList(){
- Stringtag="Main.getAPNList()";
- //current不为空表示可以使用的APN
- Stringprojection[]={"_id,apn,type,current"};
- Cursorcr=this.getContentResolver().query(uri,projection,null,null,null);
- List<APN>list=newArrayList<APN>();
- while(cr!=null&&cr.moveToNext()){
- Log.d(tag,cr.getString(cr.getColumnIndex("_id"))+""+cr.getString(cr.getColumnIndex("apn"))+""+cr.getString(cr.getColumnIndex("type"))+""+cr.getString(cr.getColumnIndex("current")));
- APNa=newAPN();
- a.id=cr.getString(cr.getColumnIndex("_id"));
- a.apn=cr.getString(cr.getColumnIndex("apn"));
- a.type=cr.getString(cr.getColumnIndex("type"));
- list.add(a);
- }
- if(cr!=null)
- cr.close();
- returnlist;
- }
- publicstaticclassAPN{
- Stringid;
- Stringapn;
- Stringtype;
- }
- }
APNMatchTools.java
Java代码
- packagecc.mdev.apn;
- /**
- *這裡是APN匹配,用於匹配移動或者聯通的APN
- *@authorSinFranciswong
- *@sitehttp://mdev.cc
- *@wikihttp://mdev.cc/wiki
- *@since2010-01-08
- *
- */
- publicfinalclassAPNMatchTools{
- publicstaticclassAPNNet{
- /**
- *中国移动cmwap
- */
- publicstaticStringCMWAP="cmwap";
- /**
- *中国移动cmnet
- */
- publicstaticStringCMNET="cmnet";
- //中国联通3GWAP设置中国联通3G因特网设置中国联通WAP设置中国联通因特网设置
- //3gwap3gnetuniwapuninet
- /**
- *3Gwap中国联通3gwapAPN
- */
- publicstaticStringGWAP_3="3gwap";
- /**
- *3Gnet中国联通3gnetAPN
- */
- publicstaticStringGNET_3="3gnet";
- /**
- *uniwap中国联通uniwapAPN
- */
- publicstaticStringUNIWAP="uniwap";
- /**
- *uninet中国联通uninetAPN
- */
- publicstaticStringUNINET="uninet";
- }
- publicstaticStringmatchAPN(StringcurrentName){
- if("".equals(currentName)||null==currentName){
- return"";
- }
- currentName=currentName.toLowerCase();
- if(currentName.startsWith(APNNet.CMNET))
- returnAPNNet.CMNET;
- elseif(currentName.startsWith(APNNet.CMWAP))
- returnAPNNet.CMWAP;
- elseif(currentName.startsWith(APNNet.GNET_3))
- returnAPNNet.GNET_3;
- elseif(currentName.startsWith(APNNet.GWAP_3))
- returnAPNNet.GWAP_3;
- elseif(currentName.startsWith(APNNet.UNINET))
- returnAPNNet.UNINET;
- elseif(currentName.startsWith(APNNet.UNIWAP))
- returnAPNNet.UNIWAP;
- elseif(currentName.startsWith("default"))
- return"default";
- elsereturn"";
- //returncurrentName.substring(0,currentName.length()-SUFFIX.length());
- }
- }
最后不要忘记加上修改APN的权限:
Xml代码
- <uses-permissionandroid:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>
经过测试在G1 上联通和移动卡均是成功的。
更多相关文章
- Android(安卓)-- ViewRoot,关于子线程刷新UI
- listview为空时,显示字符串
- Android(安卓)listview中嵌套Checkbox的布局文件
- Android之ListView属性描述
- kotlin初窥之Kotlin Android(安卓)Extensions
- Android的源代码结构
- Android应用程序启动过程源代码分析
- 安卓开发学习之012 TextView高级应用
- Android知识点记录: 使用代码设置 android 上listView的条目的点