【问题标题】:Create Network Access Point Name with code,使用代码创建网络接入点名称,
【发布时间】:2011-11-07 15:00:00
【问题描述】:

我想通过代码创建 APN,Android SDK 中是否有任何支持,我尝试了很多但没有成功,我找到了一些与此相关的信息 http://blogs.msdn.com/b/zhengpei/archive/2009/10/13/managing-apn-data-in-google-android.aspx 我使用此参考创建了一个类但无法做到任何东西,任何人都可以为此提供解决方案吗???? 谢谢

【问题讨论】:

  • 那个 MSDN 博客链接不见了。

标签: android 3g gprs apn


【解决方案1】:

我会举几个例子:

获取默认 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, 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, null, null, 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, null);
}
catch (Exception e)
{
}

所以,它必须工作,但如果不能 - 告诉我,我会尝试检查问题。

【讨论】:

  • 看来你的例子就是我要找的,我尝试将你的代码与 2.2 ANDROID emulator 一起使用,首先我将 WRITE_APN_SETTINGS 添加到 Manifest 文件中,然后尝试了它,显示 APN 的第一部分效果很好。但是当我尝试创建自己的 APN 时,我没有收到任何异常,但我看不到新的 APN。我错过了什么?谢谢
  • @ZoharAdar:我认为你定义了错误的 mcc、mnc 和数值。他们负责在 UI 页面上显示 APN。您可以使用 TelephonyManager.getNetworkOperator() 方法获取正确的值。您直接从该方法获得的数值(例如:numeric = 123210),然后从中提取 mcc 和 mnc 值(在我们的示例中:mcc = 123,mnc = 210)。
  • 在 Android 4 中,第三方应用程序无法再获得 WRITE_APN_SETTINGS 权限。因此,每次尝试添加、删除或编辑 APN 时都会出现安全异常。
  • @Borg8 运算符号是多少?
  • numeric 是您的移动运营商的 mcc 和 mnc 的串联。在这里您可以找到移动运营商代码列表:en.wikipedia.org/wiki/Mobile_country_code 例如,对于运营商 Orange Israel:mcc = 425, mnc = 01 所以 numeric = 42501。
【解决方案2】:

@Borg8 谢谢,你帮了我很多,这是我错过的,一开始我在 UI 列表中看不到新的 APN。 我在@DeepSan 上面的链接here 上找到了我的答案。

要查看我刚刚在 ** emaltor ** UI 中创建的新 APN,我使用了 310260 数字编号

// TelephonyProperties;
    values.put("mcc", "310");
    values.put("mnc", "260");
    values.put("numeric", "310260");

为了在我的设备(Galaxy)上看到它,我使用了 TelephonyManager:

  TelephonyManager tel = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    String networkOperator = tel.getNetworkOperator();
    int mcc = 0;
    int mnc = 0;
    if (networkOperator != null) {
          mcc = Integer.parseInt(networkOperator.substring(0, 3));
          mnc = Integer.parseInt(networkOperator.substring(3));
    }

   // TelephonyProperties;
    values.put("mcc", mcc );
    values.put("mnc", mnc );
    values.put("numeric",networkOperator);

现在我可以在 UI 上看到新的 APN。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    相关资源
    最近更新 更多