您好,欢迎来到爱go旅游网。
搜索
您的当前位置:首页Android 如何开关APN网络

Android 如何开关APN网络

来源:爱go旅游网
由麦可网整理,转载请注明出处

Android 如何开关APN网络

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

大家可以研究一下frameworks/base/core/Java/android/provider/Telephony.java这个类,比较重要的就是 URI 和数据库字段: content://telephony/carriers字段可以在Telephony.java中找到。

其实原理很简单 :

1、当开启APN的时候,设置一个正确的移动或者联通的APN 2、关闭的时候设置一个错误APN就会自动关闭网络

java代码:Java代码

 packageeoe.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(){

Listlist=getAPNList(); for(APNapn:list){ ContentValuescv=newContentValues();

 cv.put(\"apn\ cv.put(\"type\

 getContentResolver().update(uri,cv,\"_id=?\ }  }  

 publicvoidcloseAPN(){

 Listlist=getAPNList();  for(APNapn:list){  ContentValuescv=newContentValues();

 cv.put(\"apn\ cv.put(\"type\ getContentResolver().update(uri,cv,\"_id=?\ }  }

 

 privateListgetAPNList(){

由麦可网整理,转载请注明出处

 Stringtag=\"Main.getAPNList()\";

 //current不为空表示可以使用的APN

 Stringprojection[]={\"_id,apn,type,current\

 Cursorcr=this.getContentResolver().query(uri,projection,null,null,null);  Listlist=newArrayList();  while(cr!=null&&cr.moveToNext()){

 86.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代码:

Java代码

 packageeoe.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());  }   }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igat.cn 版权所有 赣ICP备2024042791号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务