public class SetAPN {
private static Uri APN_LIST_URI = Uri.parse("content://telephony/carriers");
private static Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
protected static String getSIMInfo() {
TelephonyManager iPhoneManager = (TelephonyManager) MyApp.getApp().getSystemService(Context.TELEPHONY_SERVICE);
return iPhoneManager.getSimOperator();
}
// public static int AddXFTApn(String apn, String user, String password, String protocol, String r_protocol, String authtype) {
// MLog.e("添加一个新的apn ==> APN");
// int apnId = -1;
// String NUM = "46003";
// String NUMERIC = getSIMInfo();
// if (NUMERIC == null) {
// return -1;
// }
// if(NUMERIC.equals(NUM)){
// NUMERIC = "46011";
// }
// ContentResolver resolver = MyApp.getApp().getContentResolver();
// ContentValues values = new ContentValues();
//
values.put("name","China Telecom"); //apn中文描述
values.put("carrier","Mytest");
// values.put("name","");
// values.put("apn", apn); //apn名称
// values.put("type", "");
// 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", user);
// values.put("server", "");
// values.put("password", password);
// values.put("localized_name", "APN_NAME_CTLTE");
// values.put("read_only", "true");
// values.put("visit_area", "460");
// values.put("protocol", protocol);
// values.put("roaming_protocol", r_protocol);
// values.put("mmsc", "");
// values.put("authtype", authtype);
// Cursor c = null;
// // 获取新添加的apn的ID
// try {
//
// Uri newRow = resolver.insert(APN_LIST_URI, values);
// if (newRow != null) {
// c = resolver.query(newRow, null, null, null, null);
// int idindex = c.getColumnIndex("_id");
// c.moveToFirst();
// apnId = c.getShort(idindex);
// MLog.e("ID = " + apnId);
// }
// } catch (SQLException e) {
// e.printStackTrace();
// }
// if (c != null)
// c.close();
// return apnId;
// }
public static void setDefaultApn(int apnId) {
ContentResolver resolver = MyApp.getApp().getContentResolver();
ContentValues values = new ContentValues();
values.put("apn_id", apnId);
MLog.e("apnId = " + apnId);
//更新当前状态APN信息
resolver.update(PREFERRED_APN_URI, values, "apn_id=?", new String[]{apnId + ""});
}
public static int AddXFTApn(String apn, String user, String password, String protocol, String r_protocol, String authtype) {
ContentResolver resolver = MyApp.getApp().getContentResolver();
String NUM = "46003";
String NUMERIC = getSIMInfo();
if (NUMERIC == null) {
return -1;
}
if (NUMERIC.equals(NUM)) {
NUMERIC = "46011";
}
MLog.d("查询到的SIM:" + NUMERIC);
ContentValues values = new ContentValues();
values.put("name", "");
values.put("apn", apn); //apn名称
values.put("type", "");
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", user);
values.put("server", "");
values.put("password", password);
values.put("localized_name", "APN_NAME_CTLTE");
values.put("read_only", "true");
values.put("visit_area", "460");
values.put("protocol", protocol);
values.put("roaming_protocol", r_protocol);
values.put("mmsc", "");
values.put("authtype", authtype);
try {
//更新数据库,替换当前数据库APN信息
int id = resolver.update(APN_LIST_URI, values, "numeric=?", new String[]{NUMERIC});
MLog.d("修改:" + id);
return id;
} catch (Exception e) {
e.printStackTrace();
MLog.d("异常:" + e.toString());
}
return -2;
}
//封装的AIDL接口
public static void setApn(String apn, String user, String password, String protocol, String r_protocol, String authtype) {
setDefaultApn(AddXFTApn(apn, user, password, protocol, r_protocol, authtype));
}
}