Android 设置APN
作者:互联网
方法一(从应用层设置):
private int CreateRasEntry(String sAPN)
{
int id = 0;
ContentResolver contentResolver = getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "中国联通");
values.put("apn", "cmnet");
values.put("numeric", "46000");
values.put("mcc", "460");
values.put("mnc", "00");
values.put("proxy", "10.0.0.172");
values.put("port", "80");
values.put("mmsc", "http://moternet.com.cn/");
values.put("current", 1);
Cursor cursor = null;
try
{
Uri uri = Uri.parse("content://telephony/carriers");
cursor = contentResolver.query(uri, new String[]{"current","_id"}, "apn=\'" + sAPN + "\'", null, null);
if(cursor!=null && cursor.getCount()>0)
{
cursor.moveToFirst();
id = cursor.getInt(cursor.getColumnIndex("_id"));
if(cursor.getInt(cursor.getColumnIndex("current"))!=1)
{
contentResolver.update(uri, values, "_id=="+id, null);
}
uri = Uri.parse("content://telephony/carriers/preferapn");
ContentResolver resolver = getContentResolver();
values = new ContentValues();
values.put("apn_id", id);
resolver.update(uri, values, null, null);
}
else
{
if(cursor!=null) cursor.close();
Uri newRow = contentResolver.insert(uri, values);
if (newRow != null)
{
cursor = contentResolver.query(newRow, null, null, null, null);
cursor.moveToFirst();
id = cursor.getInt(cursor.getColumnIndex("_id"));//取得新创建的接入点的id
uri = Uri.parse("content://telephony/carriers/preferapn");
ContentResolver resolver = getContentResolver();
values = new ContentValues();
values.put("apn_id", id);
resolver.update(uri, values, null, null);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
if (cursor != null)
cursor.close();
return id;
}
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS" /><!-- 设置APN权限 -->
方法二(从系统层设置):
find -name apns-conf.xml
在apns-conf.xml中添加以下配置:
<apn carrier="中国联通"
mcc="460"
mnc="06"
apn="cmnet"
type="default,supl,net,xcap"
protocol="IPV4V6"
roaming_protocol="IPV4V6"
/>
标签:uri,cursor,values,设置,put,APN,Android,null,id 来源: https://blog.51cto.com/u_15298588/3034540