pat*_*yts 11
您可以使用uri以编程方式查询和设置首选APN content://telephony/carriers/preferapn.要设置新的首选APN,您必须传入现有APN条目的数据库ID.(例如:如果您在APN的显示名称通过下面的函数可以做到这一点setPreferredApn(context, "Giffgaff");)
public static final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
public static final Uri APN_PREFER_URI = Uri.parse("content://telephony/carriers/preferapn");
public static boolean setPreferredApn(Context context, String name) {
boolean changed = false;
String columns[] = new String[] { Carriers._ID, Carriers.NAME };
String where = "name = ?";
String wargs[] = new String[] {name};
String sortOrder = null;
Cursor cur = context.getContentResolver().query(APN_TABLE_URI, columns, where, wargs, sortOrder);
if (cur != null) {
if (cur.moveToFirst()) {
ContentValues values = new ContentValues(1);
values.put("apn_id", cur.getLong(0));
if (context.getContentResolver().update(APN_PREFER_URI, values, null, null) == 1)
changed = true;
}
cur.close();
}
return changed;
}
Run Code Online (Sandbox Code Playgroud)
我想我应该补充一点,你需要WRITE_APN_SETTINGS权限,需要导入android.provider.Telephony和android.provider.Telephony.Carriers
更新4.0+
随着Android 4.0(ICS)的发布,该工具被禁用.启用WRITE_APN_SETTINGS权限不会影响您再次设置APN.有关相关链接,请参阅此问题.在API页面上,它现在明确声明此权限不是供外部使用,而是在内部强制执行.
| 归档时间: |
|
| 查看次数: |
10063 次 |
| 最近记录: |