public class ApnSetting extends Activity {
String TAG = "APNSetting";
//创建新APN时和查找APN列表时使用的URI
private Uri createApnUri = Uri.parse("content://telephony/carriers");
//设置默认接入点时使用的URI
private Uri preferapnUri = Uri.parse("content://telephony/carriers/preferapn");
private TextView textView = null;
private Button button = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
createApn();
showApnList();
}
});
textView = (TextView) findViewById(R.id.TextView01);
}
protected void createApn() {
// TODO Auto-generated method stub
ContentValues values = new ContentValues();
values.put("name", "CMCC CMWAP_test");
values.put("apn", "CMWAP_test");
values.put("proxy", "192.168.1.199");
values.put("port", "83");
/**
* 在真机上使用
*/
// values.put("mcc", "310");
// values.put("mnc", "995");
// values.put("numeric", "310995");
/**
* 在模拟器上使用
*/
values.put("mcc", "310");
values.put("mnc", "260");
values.put("numeric", "310260");
Uri iuri = getContentResolver().insert(createApnUri, values);
this.textView.setText("getSchema:"+iuri.getScheme()+"/getPath:"+iuri.getPath());
int id = showApnInfo(iuri);
setDefaultApn(id);
}
/**
* 设置默认APN
* @param id
*/
private void setDefaultApn(int id) {
// TODO Auto-generated method stub
ContentResolver cr = getContentResolver();
ContentValues cv = new ContentValues();
cv.put("apn_id", id);
try {
cr.update(preferapnUri, cv, null, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 显示APN信息
* @return
*/
private int showApnInfo(Uri uri) {
// TODO Auto-generated method stub
String text = "";
int id = -1;
Cursor c = this.getContentResolver().query(uri, null, null, null, null);
if(c!=null){
int colCount = c.getColumnCount();
int idIndex = c.getColumnIndex("_id");
c.moveToFirst();
id = c.getShort(idIndex);
for(int j = 0;j<colCount;j++){
text+=c.getString(j)+"|";
}
c.close();
}
return id;
}
private void showApnList() {
// TODO Auto-generated method stub
Cursor c = this.getContentResolver().query(createApnUri, null, null, null, null);
if(c!=null){
int rowCount = c.getCount();
int colCount = c.getColumnCount();
c.moveToFirst();
for(int i = 0;i<rowCount;i++){
for(int j = 0;j<colCount;j++){
Log.d(TAG, c.getColumnName(j)+"|||"+c.getString(j));
}
c.moveToNext();
}
c.close();
}
}
}