关闭APN的原理是在APN信息表(content://telephony/carriers/current )的apn, type字段添加自定义的后缀(参考自APNDroid )
package com.android.settings.widget;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
/**
*UtilityclassforchangingAPNstate
*/
public class ApnUtils{
private static final StringSUFFIX_APN = " _suffix_apn " ; // type1
private static final StringMMS = " mms " ;
private static final StringCOLUMN_ID = BaseColumns._ID;
private static final StringCOLUMN_APN = " apn " ;
private static final StringCOLUMN_TYPE = " type " ;
private static final StringCOLUMN_APN_ID = " apn_id " ; // forpreferredAPN
private static final String[]PROJECTION = new String[]{COLUMN_ID,
COLUMN_APN,COLUMN_TYPE};
private static final UriCURRENT_APNS = Uri
.parse( " content://telephony/carriers/current " );
private static final UriPREFERRED_APN = Uri
.parse( " content://telephony/carriers/preferapn " );
/**
*GetsthestateofAPN
*
* @param context
* @return trueifenabled
*/
public static boolean getApnState(Contextcontext){
ContentResolverresolver = context.getContentResolver();
int counter = 0 ;
Cursorcursor = null ;
try {
cursor = resolver.query(CURRENT_APNS,PROJECTION, null , null , null );
int typeIndex = cursor.getColumnIndex(COLUMN_TYPE);
cursor.moveToNext();
while ( ! cursor.isAfterLast()){
Stringtype = cursor.getString(typeIndex);
if (isDisabled(type)){
return false ; // noneedtocontinue
}
if ( ! isMms(type) || /* thisisMMMandwe */ shouldDisableMms()){
counter ++ ;
}
cursor.moveToNext();
}
} finally {
if (cursor != null ){
cursor.close();
}
}
return counter != 0 ;
}
private static boolean isMms(Stringtype){
return type != null && type.toLowerCase().endsWith(MMS);
}
private static boolean shouldDisableMms(){
// TODOAuto-generatedmethodstub
return false ;
}
private static boolean isDisabled(Stringvalue){
return value != null && value.endsWith(SUFFIX_APN);
}
/**
*SetAPNState
*
* @param context
* @param enabled
*/
public static void setApnState(Contextcontext, boolean enabled){
boolean shouldDisableMms = shouldDisableMms();
ContentResolverresolver = context.getContentResolver();
ContentValuesvalues = new ContentValues();
Cursorcursor = null ;
String[]args = new String[ 1 ];
try {
// COLUMN_ID{0},COLUMN_APN{1},COLUMN_TYPE{2}
cursor = resolver.query(CURRENT_APNS,PROJECTION, null , null , null );
int idIndex = cursor.getColumnIndex(COLUMN_ID);
int apnIndex = cursor.getColumnIndex(COLUMN_APN);
int typeIndex = cursor.getColumnIndex(COLUMN_TYPE);
cursor.moveToFirst();
while ( ! cursor.isAfterLast()){
StringtypeValue = cursor.getString(typeIndex);
if ( ! enabled // weshoulddisable
&& isMms(typeValue) // andthisisMMS
&& ! shouldDisableMms){ // butusersdisallowusto
// disableMMS
// ignore
cursor.moveToNext();
continue ;
}
args[ 0 ] = String.valueOf(cursor.getInt(idIndex)); // id
values.put(COLUMN_APN,getAdaptedValue(cursor.getString(apnIndex),enabled));
values.put(COLUMN_TYPE,getAdaptedValue(typeValue,enabled));
resolver.update(CURRENT_APNS,values,COLUMN_ID + " =? " ,args);
// movetonext
cursor.moveToNext();
}
} catch (Exceptione){
throw new RuntimeException(e);
} finally {
if (cursor != null ){
cursor.close();
}
}
}
private static StringgetAdaptedValue(Stringvalue, boolean enable){
final Stringmodifier = SUFFIX_APN;
// handlenull-value
if (value == null ) return enable ? value:modifier;
// removeanymodifiersothatvaluebecomesenabledinanycase
value = removeModifiers(value);
if ( ! enable){ // addrequiredmodifier
value = addModifier(value);
}
return value;
}
private static StringremoveModifiers(Stringvalue){
if (value.endsWith(SUFFIX_APN))
return value.substring( 0 ,value.length() - SUFFIX_APN.length());
else
return value;
}
private static StringaddModifier(Stringvalue){
return value + SUFFIX_APN;
}
}