安卓系统设置APN

权限

为了设置apn,我们需要在清单文件中申请权限.我开发的环境是安卓4.4.3,系统给了签名文件

1
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS" ></uses-permission>

上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import com.lubao.qhsdk.App;
/**
* Created by yangm on 2016-10-20.
*/
public class {
private static final String TAG = "ApnUtil";
public static final Uri APN_URI = Uri.parse("content://telephony/carriers");
public static final Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
public static String checkAPN() {
// 检查当前连接的APN
Cursor cr = App.self.getContentResolver().query(APN_URI, null, null, null, null);
Log.d(TAG, "cr" + cr);
String apn="";
while (cr != null && cr.moveToNext()) {
// if(cr.getString(cr.getColumnIndex("_id")))
// APN id
String id = cr.getString(cr.getColumnIndex("_id"));
Log.d(TAG, "id" + id);
// String apn_id= cr.getString(cr.getColumnIndex("apn_id"));
//
// Log.d(TAG, "apn_id" + apn_id);
// APN name
apn = cr.getString(cr.getColumnIndex("apn"));
Log.d(TAG, apn);
// Toast.makeText(getApplicationContext(),
// "当前 id:" + id + " apn:" + apn, Toast.LENGTH_LONG).show();
}
return apn;//返回apn的名字,因为我项目里要拿到当前的apn名字,,如果显示的id会很难看,像1522,根本看不懂是啥
}
// 新增一个cmnet接入点
//这里我给了个参数,就是apn的地址,,貌似只设置这个就够了,其他默认
public static int addAPN(String apnAddr) {
int id = -1;
Log.d(TAG, "添加一个新的apn");
String NUMERIC = getSIMInfo();
Log.d(TAG, "NUMERIC" + NUMERIC);
if (NUMERIC == null) {
return -1;
}
ContentResolver resolver = App.self.getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "专用APN"); // apn中文描述
values.put("apn", apnAddr); // apn名称
values.put("type", "default,supl");
values.put("numeric", NUMERIC);
values.put("mcc", NUMERIC.substring(0, 3));
values.put("mnc", NUMERIC.substring(3, NUMERIC.length()));
values.put("proxy", "");
values.put("port", "");
values.put("mmsproxy", "");
values.put("mmsport", "");
values.put("user", "");
values.put("server", "");
values.put("password", "");
values.put("mmsc", "");
Cursor c = null;
Uri newRow = resolver.insert(APN_URI, values);
if (newRow != null) {
c = resolver.query(newRow, null, null, null, null);
int idIndex = c.getColumnIndex("_id");
c.moveToFirst();
id = c.getShort(idIndex);
}
if (c != null)
c.close();
return id;
}
protected static String getSIMInfo() {
TelephonyManager iPhoneManager = (TelephonyManager) App.self
.getSystemService(Context.TELEPHONY_SERVICE);
return iPhoneManager.getSimOperator();
}
// 设置接入点
public static void setAPN(int id) {
ContentResolver resolver = App.self.getContentResolver();
ContentValues values = new ContentValues();
values.put("apn_id", id);
resolver.update(CURRENT_APN_URI, values, null, null);
// resolver.delete(url, where, selectionArgs)
}
}