Ê×Ò³ Ê«´Ê ×Öµä °å±¨ ¾ä×Ó ÃûÑÔ ÓÑ´ð ÀøÖ¾ ѧУ ÍøÕ¾µØÍ¼
µ±Ç°Î»ÖÃ: Ê×Ò³ > ½Ì³ÌƵµÀ > ÒÆ¶¯¿ª·¢ > Android >

AndroidÖ®APNÖÎÀí

2012-09-01 
AndroidÖ®APN¹ÜÀípublic class ApnSetting extends Activity {?¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡?String TAG APNSetting?¡¡¡¡¡¡¡¡

AndroidÖ®APN¹ÜÀí

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();
?¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡?}
¡¡¡¡¡¡¡¡?}
}

ÈȵãÅÅÐÐ