public class ApnUtil { private static Uri PREFERRED_APN_URI = Uri .parse("content://telephony/carriers/preferapn"); public static final String APN_ID = "apn_id"; private static final int ID_INDEX = 0; /** * get apntype * @param context * @return */ public static String getApnType(Context context){ String apntype="nomatch"; Cursor c = context.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null); if(c != null && c.getCount() >= 1){ c.moveToFirst(); String user=c.getString(c.getColumnIndex("user")); if(user.startsWith(APNNET.CTNET)){ apntype=APNNET.CTNET; }else if(user.startsWith(APNNET.CTWAP)){ apntype=APNNET.CTWAP; } } c.close(); return apntype; } public static void setSelectedApnKey(Context context,String key) { ContentResolver resolver = context.getContentResolver(); ContentValues values = new ContentValues(); values.put(APN_ID, key); resolver.update(PREFERRED_APN_URI, values, null, null); } }
另外要加上APN更改权限
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS" />