Android之APN管理

本文介绍了一个Android应用中实现自定义APN设置的方法。通过使用ContentProvider接口,该应用可以向系统添加新的APN配置,并能从系统中查询已有的APN列表。此外,还实现了设置某个APN为默认接入点的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值