APN(Access Point Name),即“接入点名称”,用来标识GPRS的业务种类,目前分为两大类:CMWAP(通过GPRS访问WAP业务)、CMNET(除了WAP以外的服务目前都用CMNET,比如连接因特网等)。在做项目的时候,客户会要求更换APN,我在frameworks/base/services/core/java/com/android/server/customized/CustomizedService.java中实现了这些接口,下面我把APN的接口展示如下:
public List selectAPN(String mNumeric){
String where = "numeric=\"" + mNumeric + "\"";
List l=new ArrayList();
Cursor cr = mContext.getContentResolver().query(mUri, null, where, null, null);
while(cr != null && cr.moveToNext()){
ContentValues values = new ContentValues();
values.put("id", cr.getString(cr.getColumnIndex("_id")));
values.put("name", cr.getString(cr.getColumnIndex("name")));
values.put("numeric", cr.getString(cr.getColumnIndex("numeric")));
values.put("mcc", cr.getString(cr.getColumnIndex("mcc")));
values.put("mnc", cr.getString(cr.getColumnIndex("mnc")));
values.put("apn", cr.getString(cr.getColumnIndex("apn")));
values.put("user", cr.getString(cr.getColumnIndex("user")));
values.put("server", cr.getString(cr.getColumnIndex("server")));
values.put("password", cr.getString(cr.getColumnIndex("password")));
values.put("proxy", cr.getString(cr.getColumnIndex("proxy")));
values.put("prot", cr.getString(cr.getColumnIndex("port")));
values.put("authtype", cr.getString(cr.getColumnIndex("authtype")));
values.put("type", cr.getString(cr.getColumnIndex("server")));
l.add(values);
}
return l;
}
public int deleteAPN(String id){
return mContext.getContentResolver().delete(mUri, "_id=" + id, null);
}
public int addAPN(String name,String numeric,String mcc,String mnc,String apn,String user,String server,String password,String proxy,String port,String authtype,String type){
int id = -1;
ContentValues values = new ContentValues();
values.put("name", name);
values.put("apn", apn);
values.put("type", type);
values.put("numeric", numeric);
values.put("mcc", mcc);
values.put("mnc", mnc);
values.put("port", port);
values.put("proxy", proxy);
values.put("user", user);
values.put("server", server);
values.put("password", password);
values.put("authtype", authtype);
Cursor c = null;
Uri newRow = mContext.getContentResolver().insert(mUri, values);
if (newRow != null) {
c = mContext.getContentResolver().query(newRow, null, null, null, null);
int idIndex = c.getColumnIndex("_id");
c.moveToFirst();
id = c.getShort(idIndex);
}
if (c != null)
c.close();
return id;
}
public int updateAPN(String id,String name,String numeric,String mcc,String mnc,String apn,String user,
String server,String password,String proxy,String port,String authtype,String type){
ContentValues values = new ContentValues();
int idback=-1;
String where = "_id=\"" + id + "\"";
values.put("name", name);
values.put("apn", apn);
values.put("type", type);
values.put("numeric", numeric);
values.put("mcc", mcc);
values.put("mnc", mnc);
values.put("port", port);
values.put("proxy", proxy);
values.put("user", user);
values.put("server", server);
values.put("password", password);
values.put("authtype", authtype);
idback=mContext.getContentResolver().update(mUri, values, where, null);
return idback;
}
public void setDefalutApn(String id) {
ContentValues values = new ContentValues();
values.put("apn_id", id);
mContext.getContentResolver().update(Uri.parse(PREFERRED_APN_URI), values, null, null);
}
接口实现如上,该导入的包不要忘记了~