在Android 6上获取“无权编写APN设置”错误

问题描述 投票:2回答:1

我的应用程序在Android 5.0及更低版本上运行正常,但是当我尝试在Android 6.0或更高版本的设备上运行它时,我收到此错误:

Writing exception to parcel
                                                 java.lang.SecurityException: No permission to write APN settings
                                                     at com.android.providers.telephony.TelephonyProvider.checkPermission(TelephonyProvider.java:4058)
                                                     at com.android.providers.telephony.TelephonyProvider.query(TelephonyProvider.java:2724)
                                                     at android.content.ContentProvider.query(ContentProvider.java:1058)
                                                     at android.content.ContentProvider$Transport.query(ContentProvider.java:245)
                                                     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
                                                     at android.os.Binder.execTransact(Binder.java:453)

任何帮助,将不胜感激。

android permissions android-6.0-marshmallow telephony securityexception
1个回答
3
投票

也许你没有在运行时获得许可。这样做

    int permissionCheck = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_APN_SETTINGS);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        //requesting permission
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_APN_SETTINGS}, 1);
    } else {
        //permission is granted and you can change APN settings
    }

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case 1: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //premission granted by user
            } else {
                //permission denied by user
            }
        }
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.