android – 使用代码创建网络访问点名称,

前端之家收集整理的这篇文章主要介绍了android – 使用代码创建网络访问点名称,前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想通过代码创建APN,在 Android SDK中是否有任何支持,我已经尝试了很多但没有成功,我发现了一些与此 http://blogs.msdn.com/b/zhengpei/archive/2009/10/13/managing-apn-data-in-google-android.aspx相关的信息我使用此参考创建了一个类,但无法做任何事情,可以请任何给出解决方案????
谢谢

解决方法

我举几个例子:

获取默认APN信息:

//path to APN table
final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");

//path to preffered APNs
final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");

//receiving cursor to preffered APN table
Cursor c = getContentResolver().query(PREFERRED_APN_URI,null,null);

//moving the cursor to beggining of the table
c.moveToFirst();

//now the cursor points to the first preffered APN and we can get some
//information about it
//for example first preffered APN id    
int index = c.getColumnIndex("_id");    //getting index of required column
Short id = c.getShort(index);           //getting APN's id from

//we can get APN name by the same way
index = c.getColumnIndex("name");
String name = c.getString(index); 

//and any other APN properties: numeric,mcc,mnc,apn,user,server,//password,proxy,port,mmsproxy,mmsport,mmsc,type,current

要定义新的APN:

//first we have to create a new row in APN table
int id = -1;
ContentResolver resolver = this.getContentResolver();
ContentValues values = new ContentValues();

//create value,you can define any other APN properties in the same way
values.put("name","Your APN Name");    //choose APN name,like 3G Orange
values.put("apn","Your APN address");  //choose APN address,like cellcom.wapu.co.il

//now we have to define APN setting page UI. You have to get operator numeric property
//you can obtain it from TelephonyManager.getNetworkOperator() method
values.put("mcc","your operator numeric high part");  //for example 242
values.put("mnc","your operator numeric low part");   //for example 501
values.put("numeric","your operator numeric");        //for example 242501

Cursor c = null;
try
{
    //insert new row to APN table
    Uri newRow = resolver.insert(APN_TABLE_URI,values);
    if(newRow != null)
    {
        c = resolver.query(newRow,null);

        //obtain the APN id
        int index = c.getColumnIndex("_id");
        c.moveToFirst();
        id = c.getShort(index);
    }
}
catch(Exception e)
{
}

//now after we created a new APN in APN table
//and APN's ID stored in id variable (or -1 if any troubles was happaned)
//we can define a new APN as default
values = new ContentValues();
values.put("apn_id",id); 

try
{
    resolver.update(PREFERRED_APN_URI,values,null);
}
catch (Exception e)
{
}

所以,它必须工作,但如果不是 – 告诉我,我会尝试检查问题.

原文链接:https://www.f2er.com/android/313587.html

猜你在找的Android相关文章