当前位置:   article > 正文

Android APN设置 获取APN列表_android9 telephony apn list

android9 telephony apn list

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

 

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

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

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

 

 

其实原理很简单 : 

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

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

 

请看代码:Activity:

  1. package cc.mdev.apn;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.content.ContentValues;
  6. import android.database.Cursor;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. /**
  13. * 這裡是Activity
  14. * @author SinFrancis wong
  15. * @site http://mdev.cc
  16. * @wiki http://mdev.cc/wiki
  17. * @since 2010-01-08
  18. */
  19. public class Main extends Activity {
  20. /** Called when the activity is first created. */
  21. Uri uri = Uri.parse("content://telephony/carriers");
  22. @Override
  23. public void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.main);
  26. Button open= (Button) findViewById(R.id.open);
  27. Button close= (Button) findViewById(R.id.close);
  28. open.setOnClickListener(new View.OnClickListener() {
  29. @Override
  30. public void onClick(View v) {
  31. openAPN();
  32. }
  33. });
  34. close.setOnClickListener(new View.OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. closeAPN();
  38. }
  39. });
  40. }
  41. public void openAPN(){
  42. List<APN> list = getAPNList();
  43. for (APN apn : list) {
  44. ContentValues cv = new ContentValues();
  45. cv.put("apn", APNMatchTools.matchAPN(apn.apn));
  46. cv.put("type", APNMatchTools.matchAPN(apn.type));
  47. getContentResolver().update(uri, cv, "_id=?", new String[]{apn.id});
  48. }
  49. }
  50. public void closeAPN(){
  51. List<APN> list = getAPNList();
  52. for (APN apn : list) {
  53. ContentValues cv = new ContentValues();
  54. cv.put("apn", APNMatchTools.matchAPN(apn.apn)+"mdev");
  55. cv.put("type", APNMatchTools.matchAPN(apn.type)+"mdev");
  56. getContentResolver().update(uri, cv, "_id=?", new String[]{apn.id});
  57. }
  58. }
  59. private List<APN> getAPNList(){
  60. String tag = "Main.getAPNList()";
  61. //current不为空表示可以使用的APN
  62. String projection[] = {"_id,apn,type,current"};
  63. Cursor cr = this.getContentResolver().query(uri, projection, null, null, null);
  64. List<APN> list = new ArrayList<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. APN a = new APN();
  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. return list;
  76. }
  77. public static class APN{
  78. String id;
  79. String apn;
  80. String type;
  81. }
  82. }

APNMatchTools.java

  1. package cc.mdev.apn;
  2. /**
  3. * 這裡是APN匹配,用於匹配移動或者聯通的APN
  4. * @author SinFrancis wong
  5. * @site http://mdev.cc
  6. * @wiki http://mdev.cc/wiki
  7. * @since 2010-01-08
  8. *
  9. */
  10. public final class APNMatchTools {
  11. public static class APNNet{
  12. /**
  13. * 中国移动cmwap
  14. */
  15. public static String CMWAP = "cmwap";
  16. /**
  17. * 中国移动cmnet
  18. */
  19. public static String CMNET = "cmnet";
  20. //中国联通3GWAP设置 中国联通3G因特网设置 中国联通WAP设置 中国联通因特网设置
  21. //3gwap 3gnet uniwap uninet
  22. /**
  23. * 3G wap 中国联通3gwap APN
  24. */
  25. public static String GWAP_3 = "3gwap";
  26. /**
  27. * 3G net 中国联通3gnet APN
  28. */
  29. public static String GNET_3="3gnet";
  30. /**
  31. * uni wap 中国联通uni wap APN
  32. */
  33. public static String UNIWAP="uniwap";
  34. /**
  35. * uni net 中国联通uni net APN
  36. */
  37. public static String UNINET="uninet";
  38. }
  39. public static String matchAPN(String currentName) {
  40. if("".equals(currentName) || null==currentName){
  41. return "";
  42. }
  43. currentName = currentName.toLowerCase();
  44. if(currentName.startsWith(APNNet.CMNET))
  45. return APNNet.CMNET;
  46. else if(currentName.startsWith(APNNet.CMWAP))
  47. return APNNet.CMWAP;
  48. else if(currentName.startsWith(APNNet.GNET_3))
  49. return APNNet.GNET_3;
  50. else if(currentName.startsWith(APNNet.GWAP_3))
  51. return APNNet.GWAP_3;
  52. else if(currentName.startsWith(APNNet.UNINET))
  53. return APNNet.UNINET;
  54. else if(currentName.startsWith(APNNet.UNIWAP))
  55. return APNNet.UNIWAP;
  56. else if(currentName.startsWith("default"))
  57. return "default";
  58. else return "";
  59. // return currentName.substring(0, currentName.length() - SUFFIX.length());
  60. }
  61. }
APN权限
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission> 


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

content://telephony/carriers                                  //取得全部apn列表
content://telephony/carriers/preferapn                   //取得当前设置的apn
content://telephony/carriers/current                      //取得current=1的apn列表

  1. public class APNList extends ListActivity implements OnItemClickListener{
  2. public static final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
  3. public static final Uri PREFERRED_APN_URI =Uri.parse("content://telephony/carriers/preferapn");
  4. public static final Uri CURRENT_APN_URI =Uri.parse("content://telephony/carriers/current");
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. Cursor cr = this.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
  9. String[] mStrings=new String[cr.getCount()];
  10. int i=0;
  11. while(cr!=null && cr.moveToNext()){
  12. mStrings[i]="ID:"+cr.getString(cr.getColumnIndex("_id"))+
  13. "\n"+"name:"+cr.getString(cr.getColumnIndex("name"))+
  14. "\n"+"numeric:"+cr.getString(cr.getColumnIndex("numeric"))+
  15. "\n"+"mcc:"+cr.getString(cr.getColumnIndex("mcc"))+
  16. "\n"+"mnc:"+cr.getString(cr.getColumnIndex("mnc"))+
  17. "\n"+"apn:"+cr.getString(cr.getColumnIndex("apn"))+
  18. "\n"+"user:"+cr.getString(cr.getColumnIndex("user"))+
  19. "\n"+"server:"+cr.getString(cr.getColumnIndex("server"))+
  20. "\n"+"password:"+cr.getString(cr.getColumnIndex("password"))+
  21. "\n"+"proxy:"+cr.getString(cr.getColumnIndex("proxy"))+
  22. "\n"+"port:"+cr.getString(cr.getColumnIndex("port"))+
  23. "\n"+"mmsproxy:"+cr.getString(cr.getColumnIndex("mmsproxy"))+
  24. "\n"+"mmsport:"+cr.getString(cr.getColumnIndex("mmsport"))+
  25. "\n"+"mmsc:"+cr.getString(cr.getColumnIndex("mmsc"))+
  26. "\n"+"authtype:"+cr.getString(cr.getColumnIndex("authtype"))+
  27. "\n"+"type:"+cr.getString(cr.getColumnIndex("type"))+
  28. "\n"+"current:"+cr.getString(cr.getColumnIndex("current"));
  29. i++;
  30. }
  31. setListAdapter(new ArrayAdapter<String>(this,
  32. android.R.layout.simple_list_item_1, mStrings));
  33. getListView().setOnItemClickListener(this);
  34. }
  35. @Override
  36. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  37. // TODO Auto-generated method stub
  38. SetDefaultAPN(arg2+1);
  39. }
  40. /*
  41. * Set an apn to be the default apn for web traffic
  42. * Require an input of the apn id to be set
  43. */
  44. public boolean SetDefaultAPN(int id)
  45. {
  46. boolean res = false;
  47. ContentResolver resolver = getContentResolver();
  48. ContentValues values = new ContentValues();
  49. //See /etc/apns-conf.xml. The TelephonyProvider uses this file to provide
  50. //content://telephony/carriers/preferapn URI mapping
  51. values.put("apn_id", id);
  52. Log.d("", "change1:");
  53. try
  54. {
  55. int cc=resolver.update(PREFERRED_APN_URI, values, null, null);
  56. Log.d("", "change:"+cc);
  57. }
  58. catch (SQLException e)
  59. {
  60. Log.d("", "change"+e.getMessage());
  61. }
  62. return res;
  63. }
  64. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/988213
推荐阅读
相关标签
  

闽ICP备14008679号