设置APN为cmnet源码

本文介绍了一个用于Android系统的APN设置与管理的Java类。该类提供了检查当前APN设置、添加新的APN配置(如cmnet)并将其设置为当前APN的功能。通过ContentResolver和SQLite操作实现。

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

public class APNActivity extends Activity {

public static final Uri APN_URI = Uri.parse("content://telephony/carriers");
public static final Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/preferapn");

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int _cmnetId = addAPN();
SetAPN(_cmnetId);
}
public void checkAPN(){
// 检查当前连接的APN
Cursor cr = getContentResolver().query(CURRENT_APN_URI, null, null,
null, null);
while (cr != null && cr.moveToNext()) {
// APN id
String id = cr.getString(cr.getColumnIndex("_id"));
// APN name
String apn = StringUtils.null2String(cr
.getString(cr.getColumnIndex("apn")));
//Toast.makeText(getApplicationContext(),
//&当前 id:" + id + " apn:" + apn, Toast.LENGTH_LONG).show();

}
//新增一个cmnet接入点
public int addAPN() {
int id = -1;
ContentResolver resolver = this.getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "cmnet");
values.put("apn", "cmnet");
Cursor c = null;
Uri newRow = resolver.insert(APN_URI, values);
if (newRow != null) {
c = resolver.query(newRow, null, null, null, null);
int idIndex = c.getColumnIndex("_id");
c.moveToFirst();
id = c.getShort(idIndex);
}
if (c != null)
c.close();
return id;
}
//设置接入点
public void SetAPN(int id) {
ContentResolver resolver = this.getContentResolver();
ContentValues values = new ContentValues();
values.put("apn_id", id);
resolver.update(CURRENT_APN_URI, values, null, null);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值