首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

怎么修改android手机apn的值

2012-08-13 
如何修改android手机apn的值首先先得到当前的apn..对当前的APN进行判断String resultApn getCurrentAPNF

如何修改android手机apn的值

首先先得到当前的apn..对当前的APN进行判断

String resultApn = getCurrentAPNFromSetting(getContentResolver());
?????if (!resultApn.equals("")) {
??????if (!resultApn.equals("cmwap")) {
???????int apnCode = updateCurrentAPN(
?????????getContentResolver(), "cmwap");
??????}

}

然后在修改APN为你想要的模式,apnCode的值为1则修改成功!本地测试通过...
/**
? * 得到当前的apn
? *
? * @param resolver
? * @return
? */
?public static String getCurrentAPNFromSetting(ContentResolver resolver) {
??Cursor cursor = null;
??try {
???cursor = resolver.query(CURRENT_APN_URI, null, null, null, null);
???String curApnId = null;
???if (cursor != null && cursor.moveToFirst()) {
????curApnId = cursor.getString(cursor.getColumnIndex("_id"));
????String apnName1 = cursor
??????.getString(cursor.getColumnIndex("apn"));
???}
???cursor.close();
???// find apn name from apn list
???if (curApnId != null) {
????cursor = resolver.query(APN_LIST_URI, null, " _id = ?",
??????new String[] { curApnId }, null);
????if (cursor != null && cursor.moveToFirst()) {
?????String apnName = cursor.getString(cursor
???????.getColumnIndex("apn"));
?????return apnName;
????}
???}

??} catch (SQLException e) {
??} finally {
???if (cursor != null) {
????cursor.close();
???}
??}

??return null;
?}

?/**
? * 修改当年的apn
? *
? * @param resolver
? * @param newAPN
? * @return
? */
?public static int updateCurrentAPN(ContentResolver resolver, String newAPN) {
??Cursor cursor = null;
??try {
???// get new apn id from list
???cursor = resolver.query(APN_LIST_URI, null,
?????" apn = ? and current = 1",
?????new String[] { newAPN.toLowerCase() }, null);
???String apnId = null;
???if (cursor != null && cursor.moveToFirst()) {
????apnId = cursor.getString(cursor.getColumnIndex("_id"));
???}
???cursor.close();

???// set new apn id as chosen one
???if (apnId != null) {
????ContentValues values = new ContentValues();
????values.put("apn_id", apnId);
????resolver.update(CURRENT_APN_URI, values, null, null);
???} else {
????// apn id not found, return 0.
????return 0;
???}
??} catch (SQLException e) {

??} finally {
???if (cursor != null) {
????cursor.close();
???}
??}

??// update success
??return 1;
?}

热点排行