Android Apn 是由telephony.db來進行設定的,如果要改成預設的Apn就將Type 設為default 再將此Apn設為預設的Apn即可
下面提供常用方法
public static final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
public static final Uri PREFERRED_APN_URI =Uri.parse("content://telephony/carriers/preferapn");
/**
* 列舉所有Apn
*/
private void EnumerateAPNs()
{
ApnList = new ArrayList<String>();
/*for (int i=0; i< 20; i++)
{
ApnList.add("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx "+i);
}*/
Cursor c = TestApn.this.getContentResolver().query(
APN_TABLE_URI, null, null, null, null);
if (c != null)
{
/*
* Fields you can retrieve can be found in
com.android.providers.telephony.TelephonyProvider :
db.execSQL("CREATE TABLE " + CARRIERS_TABLE +
"(_id INTEGER PRIMARY KEY," +
"name TEXT," +
"numeric TEXT," +
"mcc TEXT," +
"mnc TEXT," +
"apn TEXT," +
"user TEXT," +
"server TEXT," +
"password TEXT," +
"proxy TEXT," +
"port TEXT," +
"mmsproxy TEXT," +
"mmsport TEXT," +
"mmsc TEXT," +
"authtype TEXT," +
"type TEXT," +
"current INTEGER);");
*/
String s = "All APNs:\n";
Log.d(TAG, s);
try
{
s += printAllData(c); //Print the entire result set
}
catch(SQLException e)
{
Log.d(TAG, e.getMessage());
}
//Log.d(TAG, "listview: "+s + "\n\n");
writeinfo = s;
writeFile();
c.close();
}
}
private String printAllData(Cursor c)
{
if(c == null) return null;
String s = "";
int record_cnt = c.getColumnCount();
Log.d(TAG, "Total # of records: " + record_cnt);
if(c.moveToFirst())
{
String[] columnNames = c.getColumnNames();
Log.d(TAG,getAllColumnNames(columnNames));
s += getAllColumnNames(columnNames);
do{
String row = "";
for(String columnIndex:columnNames)
{
int i = c.getColumnIndex(columnIndex);
row += c.getString(i)+"&;&\t";
}
row += "\n";
Log.d(TAG, row);
ApnList.add(row);
s += row;
}while(c.moveToNext());
Log.d(TAG,"End Of Records");
}
return s;
}
private String getAllColumnNames(String[] columnNames)
{
String s = "Column Names:\n";
for(String t:columnNames)
{
s += t + ":\t";
}
return s+"\n";
}
設為預設Apn的方法
private void setDefApnProcess(int apnid)
{
UpdateDefApn(apnid);
SetDefaultAPN(apnid);
}
/**
* 更新為預設Apn
* @param apnid
*/
public void UpdateDefApn(int apnid)
{
int id = -1;
ContentResolver resolver = TestApn.this.getContentResolver();
ContentValues values = new ContentValues();
values.put("type", "default");
Cursor c = null;
try
{
//Uri newRow = resolver.insert(APN_TABLE_URI, values);
int newRow = resolver.update(APN_TABLE_URI, values, "_id='"+apnid+"'", null);
if (newRow != 0)
{
Log.d(TAG, "update row: "+newRow);
Log.d(TAG, "update complete");
}
}
catch(SQLException e)
{
Log.d(TAG, e.getMessage());
}
if(c !=null )
c.close();
}
/**
* 設為預設Apn
*/
public boolean SetDefaultAPN(int id)
{
boolean res = false;
ContentResolver resolver = TestApn.this.getContentResolver();
ContentValues values = new ContentValues();
//See /etc/apns-conf.xml. The TelephonyProvider uses this file to provide
//content://telephony/carriers/preferapn URI mapping
values.put("apn_id", id);
try
{
resolver.update(PREFERRED_APN_URI, values, null, null);
Cursor c = resolver.query(
PREFERRED_APN_URI,
new String[]{"name","apn"},
"_id="+id,
null,
null);
if(c != null)
{
res = true;
c.close();
}
Log.d(TAG, "set defApn ok");
Toast.makeText(TestApn.this, "set Default APN complete", Toast.LENGTH_SHORT).show();
}
catch (SQLException e)
{
Log.d(TAG, e.getMessage());
}
return res;
}
留言列表