从代码[root]中读取当前APN名称

时间:2016-05-20 09:33:25

标签: android apn

我需要能够读取当前的APN名称。我的应用程序是一个系统应用程序(它位于/ system / app下),我有root访问权限。

我正在尝试获取APN名称,但这是不可能的,因为我总是被提示:

No permission to write APN settings

我还在Android Manifest中添加了以下权限

<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"/>

TARGET SDK&gt; 18(棒棒糖)

非常感谢。

1 个答案:

答案 0 :(得分:0)

WRITE_APN_SETTINGS

不供第三方应用程序使用。 您无法在API > 18中阅读APN设置。

适用于API&lt; = 18

public class APNHelper {

    private Context context;

    public APNHelper(final Context context) {
        this.context = context;
    }

    @SuppressWarnings("unchecked")
    public List<APN> getMMSApns() {
        final Cursor apnCursor = this.context.getContentResolver()
                .query(Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI,
                        "current"), null, null, null, null);
        if (apnCursor == null) {
            return Collections.EMPTY_LIST;
        } else {
            final List<APN> results = new ArrayList<APN>();
            if (apnCursor.moveToFirst()) {
                do {
                    final String type = apnCursor.getString(apnCursor
                            .getColumnIndex(Telephony.Carriers.TYPE));
                    if (!TextUtils.isEmpty(type)
                            && (type.equalsIgnoreCase("*") || type
                            .equalsIgnoreCase("mms"))) {
                        final String mmsc = apnCursor.getString(apnCursor
                                .getColumnIndex(Telephony.Carriers.MMSC));
                        final String mmsProxy = apnCursor.getString(apnCursor
                                .getColumnIndex(Telephony.Carriers.MMSPROXY));
                        final String port = apnCursor.getString(apnCursor
                                .getColumnIndex(Telephony.Carriers.MMSPORT));
                        final APN apn = new APN();
                        apn.MMSCenterUrl = mmsc;
                        apn.MMSProxy = mmsProxy;
                        apn.MMSPort = port;
                        results.add(apn);

                        Toast.makeText(context,
                                mmsc + " " + mmsProxy + " " + port,
                                Toast.LENGTH_LONG).show();
                    }
                } while (apnCursor.moveToNext());
            }
            apnCursor.close();
            return results;
        }
    }
}