Android APN

Android APN

1 // APN列表资源
2 private static Uri APN_LIST_URI = Uri.parse("content://telephony/carriers");
1 // 默认APN资源(当前正在使用的apn)
2 private static Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
1 int id = -1;
2 ContentResolver resolver = context.getContentResolver();
3 Cursor c = resolver.query(APN_LIST_URI, new String[] { "_id", "name", "apn" }, "apn like '%myapn%'", null, null); //从apn列表中查找apn名称为myapn的apn信息
4 if (c != null && c.moveToNext()) {
5    id = c.getShort(c.getColumnIndex("_id")); //获取该apn的id信息
6    System.out.println("APN已经存在");
7 } else {
8    System.out.println("APN不存在");        
9 }
 1 protected String getSIMInfo() {
 2     TelephonyManager iPhoneManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
 3     return iPhoneManager.getSimOperator();
 4 }
 5 
 6 
 7 public int AddXFTApn() {
 8     System.out.println("添加一个新的apn ==> XFT APN");
 9     int apnId = -1;
10     String NUMERIC = getSIMInfo();
11     if (NUMERIC == null) {
12         Toast.makeText(context, "不存在SIM卡", Toast.LENGTH_SHORT).show();
13         return -1;
14     }
15     ContentResolver resolver = context.getContentResolver();
16     ContentValues values = new ContentValues();
17 
18     values.put("name",“专用APN”); //apn中文描述
19     values.put("apn", “myapn”);  //apn名称
20     values.put("type", "default,supl");
21     values.put("numeric", NUMERIC);
22     values.put("mcc", NUMERIC.substring(0, 3));
23     values.put("mnc", NUMERIC.substring(3, NUMERIC.length()));
24     values.put("proxy", "");
25     values.put("port", "");
26     values.put("mmsproxy", "");
27     values.put("mmsport", "");
28     values.put("user", "");
29     values.put("server", "");
30     values.put("password", "");
31     values.put("mmsc", "");
32     Cursor c = null;
33     // 获取新添加的apn的ID
34     try {
35         Uri newRow = resolver.insert(APN_LIST_URI, values);
36         if (newRow != null) {
37             c = resolver.query(newRow, null, null, null, null);
38             int idindex = c.getColumnIndex("_id");
39             c.moveToFirst();
40             apnId = idindex;
41         }
42     } catch (SQLException e) {
43         e.printStackTrace();
44     }
45     if (c != null)
46         c.close();
47     return apnId;
48 }
 1 public boolean setDefaultApn(int apnId) { 
 2     //  System.out.println("设置选中的apnID ==》 " + apnId);
 3     boolean res = false;
 4     ContentResolver resolver = context.getContentResolver();
 5     ContentValues values = new ContentValues();
 6     values.put("apn_id", apnId);
 7     try {
 8         resolver.update(PREFERRED_APN_URI, values, null, null);
 9         Cursor c = resolver.query(PREFERRED_APN_URI, new String[] { "name", "apn" }, "_>);
10         if (c != null) {
11             res = true;
12             c.close();
13         }
14     } catch (SQLException e) {
15         e.printStackTrace();
16     }
17     return res;
18 }
 1 protected String getSIMInfo() {
 2  2     TelephonyManager iPhoneManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
 3  3     return iPhoneManager.getSimOperator();
 4  4 }
 5  5 
 6  6 
 7  7 public int AddXFTApn() {
 8  8     System.out.println("添加一个新的apn ==> XFT APN");
 9  9     int apnId = -1;
10 10     String NUMERIC = getSIMInfo();
11 11     if (NUMERIC == null) {
12 12         Toast.makeText(context, "不存在SIM卡", Toast.LENGTH_SHORT).show();
13 13         return -1;
14 14     }
15 15     ContentResolver resolver = context.getContentResolver();
16 16     ContentValues values = new ContentValues();
17 17 
18 18     values.put("name",“专用APN”); //apn中文描述
19 19     values.put("apn", “myapn”);  //apn名称
20 20     values.put("type", "default,supl");
21 21     values.put("numeric", NUMERIC);
22 22     values.put("mcc", NUMERIC.substring(0, 3));
23 23     values.put("mnc", NUMERIC.substring(3, NUMERIC.length()));
24 24     values.put("proxy", "");
25 25     values.put("port", "");
26 26     values.put("mmsproxy", "");
27 27     values.put("mmsport", "");
28 28     values.put("user", "");
29 29     values.put("server", "");
30 30     values.put("password", "");
31 31     values.put("mmsc", "");
32 32     Cursor c = null;
33 33     // 获取新添加的apn的ID
34 34     try {
35 35         Uri newRow = resolver.insert(APN_LIST_URI, values);
36 36         if (newRow != null) {
37 37             c = resolver.query(newRow, null, null, null, null);
38 38             int idindex = c.getColumnIndex("_id");
39 39             c.moveToFirst();
40 40             apnId = idindex;
41 41         }
42 42     } catch (SQLException e) {
43 43         e.printStackTrace();
44 44     }
45 45     if (c != null)
46 46         c.close();
47 47     return apnId;
48 48 }
View Code