在Android4.0之后,设置APN需要系统级别的APP才可以。因为系统是自己做的,所以这里我附上一个我所使用的方法,给有需要的人。
1.添加权限:<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"/>
public static final Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
public int addAPN(String name,String apn) {
int id = -1;
ContentResolver resolver = this.getContentResolver();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("apn", apn);
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;
}
ContentResolver resolver = this.getContentResolver();
ContentValues values = new ContentValues();
values.put("apn_id", id);
resolver.update(CURRENT_APN_URI, values, null, null);
}
5.这样就添加完毕了,接下来提供一个查询APN的方法。
public void checkAPN(){
// 检查当前连接的APN
Cursor cr = getContentResolver().query(CURRENT_APN_URI, null, null,
null, null);
while (cr != null && cr.moveToNext()) {
// APN id
String id = cr.getString(cr.getColumnIndex("_id"));
// APN name
String apn = (String)(cr.getString(cr.getColumnIndex("apn")));
Toast.makeText(getApplicationContext(),
"当前 id:" + id + " apn:" + apn, Toast.LENGTH_LONG).show();
}
}
不过这个方法只能查询到目前你所使用的APN。