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

时间:2016-12-17 21:01:24

标签: android permissions android-6.0-marshmallow telephony securityexception

我的应用程序在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)

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分: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);
    }
}