凉白开 2011-05-27
publicclassAPNActivityextendsActivity{
publicstaticfinalUriAPN_URI=Uri.parse("content://telephony/carriers");
publicstaticfinalUriCURRENT_APN_URI=Uri.parse("content://telephony/carriers/preferapn");
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int_cmnetId=addAPN();
SetAPN(_cmnetId);
}
publicvoidcheckAPN(){
//检查当前连接的APN
Cursorcr=getContentResolver().query(CURRENT_APN_URI,null,null,
null,null);
while(cr!=null&&cr.moveToNext()){
//APNid
Stringid=cr.getString(cr.getColumnIndex("_id"));
//APNname
Stringapn=StringUtils.null2String(cr
.getString(cr.getColumnIndex("apn")));
//Toast.makeText(getApplicationContext(),
//&当前id:"+id+"apn:"+apn,Toast.LENGTH_LONG).show();
}
//新增一个cmnet接入点
publicintaddAPN(){
intid=-1;
ContentResolverresolver=this.getContentResolver();
ContentValuesvalues=newContentValues();
values.put("name","cmnet");
values.put("apn","cmnet");
Cursorc=null;
UrinewRow=resolver.insert(APN_URI,values);
if(newRow!=null){
c=resolver.query(newRow,null,null,null,null);
intidIndex=c.getColumnIndex("_id");
c.moveToFirst();
id=c.getShort(idIndex);
}
if(c!=null)
c.close();
returnid;
}
//设置接入点
publicvoidSetAPN(intid){
ContentResolverresolver=this.getContentResolver();
ContentValuesvalues=newContentValues();
values.put("apn_id",id);
resolver.update(CURRENT_APN_URI,values,null,null);
}
}