public class APNActivity extends Activity {
/***
* 是手机中获取所有APN的uri
* 列出全部的APN
*/
public static final Uri APN_URI = Uri.parse("content://telephony/carriers");
/***
* 是手机默认调用的APN的URI
* 获取当前使用的APN
*/
public static final Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int _3gWapId = addAPN();
SetAPN(_3gWapId);
}
//新增一个3GWap接入点
public int addAPN() {
int id = -1;
ContentResolver resolver = this.getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "3gwap");
values.put("apn", "3gwap");
values.put("mcc", "460");
values.put("mnc", "01");
values.put("numeric", "46001");
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;
}
//设置接入点
public void SetAPN(int id) {
ContentResolver resolver = this.getContentResolver();
ContentValues values = new ContentValues();
values.put("apn_id", id);
resolver.update(CURRENT_APN_URI, values, null, null);
}
}s
权限 <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>