Android:设置APN为cmnet源码 ----android 4.0之后需要系统签名才能添加

01 public class APNActivity extends Activity {
02  
03         public static final Uri APN_URI = Uri.parse("content://telephony/carriers");
04         public static final Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
05  
06         @Override
07         public void onCreate(Bundle savedInstanceState) {
08                 super.onCreate(savedInstanceState);
09                 setContentView(R.layout.main);
10                 int _cmnetId = addAPN();
11                 SetAPN(_cmnetId);
12         }
01        public void checkAPN(){
02       // 检查当前连接的APN
03               Cursor cr = getContentResolver().query(CURRENT_APN_URI, nullnull,
04               nullnull);
05               while (cr != null && cr.moveToNext()) {
06                   // APN id
07                   String id = cr.getString(cr.getColumnIndex("_id"));
08                   // APN name
09                   String apn = StringUtils.null2String(cr
10                   .getString(cr.getColumnIndex("apn")));
11                   // Toast.makeText(getApplicationContext(),
12                   // "当前 id:" + id + " apn:" + apn, Toast.LENGTH_LONG).show();
13  
14        }
15  
16         //新增一个cmnet接入点
17         public int addAPN() {
18                 int id = -1;
19                 ContentResolver resolver = this.getContentResolver();
20                 ContentValues values = new ContentValues();
21                 values.put("name""cmnet");
22                 values.put("apn""cmnet");
23                 Cursor c = null;
24                 Uri newRow = resolver.insert(APN_URI, values);
25                 if (newRow != null) {
26                         c = resolver.query(newRow, nullnullnullnull);
27                         int idIndex = c.getColumnIndex("_id");
28                         c.moveToFirst();
29                         id = c.getShort(idIndex);
30                 }
31                 if (c != null)
32                         c.close();
33                 return id;
34         }
35         //设置接入点
36         public void SetAPN(int id) {
37                 ContentResolver resolver = this.getContentResolver();
38                 ContentValues values = new ContentValues();
39                 values.put("apn_id", id);
40                 resolver.update(CURRENT_APN_URI, values, nullnull);
41         }
42 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值