Android APN 执行

发布于 2024-12-11 05:36:55 字数 63 浏览 0 评论 0原文

有人知道是否有一种以编程方式在设备上使用特定定义的 APN(不是默认的 APN)的方法?

谢谢。

Someone know if there's a programmatically way to use a specific defined APN on the device which is not the default one?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揪着可爱 2024-12-18 05:36:55

您可以使用 uri content://telephony/riers/preferapn 以编程方式查询和设置首选 APN。要设置新的首选 APN,您必须传入现有 APN 条目的数据库 ID。如果您传入 APN 的显示名称,以下函数可以执行此操作(例如:setPreferredApn(context, "Giffgaff");

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

public static boolean setPreferredApn(Context context, String name) {
    boolean changed = false;
    String columns[] = new String[] { Carriers._ID, Carriers.NAME };
    String where = "name = ?";
    String wargs[] = new String[] {name};
    String sortOrder = null;
    Cursor cur = context.getContentResolver().query(APN_TABLE_URI, columns, where, wargs, sortOrder);
    if (cur != null) {
        if (cur.moveToFirst()) {
            ContentValues values = new ContentValues(1);
            values.put("apn_id", cur.getLong(0));
            if (context.getContentResolver().update(APN_PREFER_URI, values, null, null) == 1)
                changed = true;
        }
        cur.close();
    }
    return changed;
}

我想我应该补充一点,您需要 WRITE_APN_SETTINGS 权限并且需要导入android.provider.Telephony 和 android.provider.Telephony.Carriers

4.0+ 更新

此功能随着 Android 的发布而被禁用4.0(工业控制系统)。启用 WRITE_APN_SETTINGS 权限不会影响您再设置 APN。请参阅此问题获取一些相关链接。在 API 页面上,现在明确声明此权限不供外部使用,这是内部强制执行的。

You can programmatically query and set the preferred APN using the uri content://telephony/carriers/preferapn. To set a new preferred APN you have to pass in the database ID of an existing APN entry. The following function can do this if you pass in the display name of the APN (eg: setPreferredApn(context, "Giffgaff");)

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

public static boolean setPreferredApn(Context context, String name) {
    boolean changed = false;
    String columns[] = new String[] { Carriers._ID, Carriers.NAME };
    String where = "name = ?";
    String wargs[] = new String[] {name};
    String sortOrder = null;
    Cursor cur = context.getContentResolver().query(APN_TABLE_URI, columns, where, wargs, sortOrder);
    if (cur != null) {
        if (cur.moveToFirst()) {
            ContentValues values = new ContentValues(1);
            values.put("apn_id", cur.getLong(0));
            if (context.getContentResolver().update(APN_PREFER_URI, values, null, null) == 1)
                changed = true;
        }
        cur.close();
    }
    return changed;
}

I guess I should add that you need WRITE_APN_SETTINGS permission and need to import android.provider.Telephony and android.provider.Telephony.Carriers

UPDATE FOR 4.0+

This facility became disabled with the release of Android 4.0 (ICS). Enabling the WRITE_APN_SETTINGS permission has no effect on allowing you to set the APN any more. See this question for some relevant links. On the API page it now states explicitly this permission is not for external use and this is enforced internally.

抠脚大汉 2024-12-18 05:36:55

我认为没有办法,即使有,运营商也可以通过软件更新来消除它。此外,对于美国 AT&T 等一些运营商来说,使用特定的 APN 可以提供特定的功能,例如获取该用户的订户号码(它是唯一的 ID,而不是电话号码)。因此,强制进行此更改可能不是一个好主意,因为它将影响手机上安装的许多其他应用程序。

I dont think there is a way, even if there is one, the carrier could wipe it out with a software update. Also, for some carriers like AT&T in US, using a specific APN provides specific functionality, like getting the Subscriber number of that user (its a unique ID, not the phone number). So it may not be a good idea to force this change, as it will impact numerous other apps installed on handset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文