【问题】【权限】java.lang.SecurityException: Permission Denial

1.问题现象

在AndroidManifest.xml里虽然申请了权限android.permission.READ_CONTACTS,但是在访问联系人contentprovider时候总报下面错误:
W/System.err: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{7f79917 20903:com.example.contractstest/u0a111} (pid=20903, uid=10111) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS

Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActivityManagerService.getContentProviderImpl(ActivityManagerService.java:12188)

2.原因

从Android 6版本后,安卓对权限控制更加严格,不仅要在AndroidManifest.xml里写明要申请的权限,还要在代码里申请权限,从而需要在代码里对不同的sdk版本做判断操作

3.解决办法

在代码里加入对权限的判断,如下,当sdk版本是23后的,而且利用context.checkSelfPermission函数检查自身程序没有READ_CONTACTS权限的话,执行activity.requestPermissions(permissions, requestCode)。此时界面上会弹出提示是否允许该权限,当用户选择允许后,系统执行回调函数onRequestPermissionsResult,后面就有权限READ_CONTACTS

    ListView contractsView;

    ArrayAdapter<String> adapter;

    List<String> contractList = new ArrayList<String>();

    // Request code for READ_CONTACTS. It can be any number > 0.
    private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        contractsView = (ListView) findViewById(R.id.contracts_view);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contractList);
        contractsView.setAdapter(adapter);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            Log.d("MainActivity",String.valueOf(Build.VERSION.SDK_INT));
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
            //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
        } else {
            readContracts();
        }


    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission is granted
                readContracts();
                adapter.notifyDataSetChanged();
            } else {
                Toast.makeText(this, "Until you grant the permission, we cannot display the names", Toast.LENGTH_LONG).show();
            }
        }
    }

注解:
Build.VERSION.SDK_INT 表示在硬件设备上运行的sdk版本,返回的是int格式
Build.VERSION_CODES.M 表示安卓的棉花糖版本,也就是Android 6

4.version参考

安卓帮助地址:
https://developer.android.google.cn/reference

Build.VERSION

Kotlin |Java
public static class Build.VERSION
extends Object

java.lang.Object
↳ android.os.Build.VERSION
public static final String BASE_OS
The base OS build the product is based on.

public static final String BASE_OS
The base OS build the product is based on.

public static final String CODENAME
The current development codename, or the string “REL” if this is a release build.

public static final String INCREMENTAL
The internal value used by the underlying source control to represent this build.

public static final int PREVIEW_SDK_INT
The developer preview revision of a prerelease SDK.

public static final String RELEASE
The user-visible version string.

public static final String SDK
This field was deprecated in API level 15. Use SDK_INT to easily get this as an integer.

public static final int SDK_INT
The SDK version of the software currently running on this hardware device.

public static final String SECURITY_PATCH
The user-visible security patch level.

Build.VERSION_CODES

Kotlin |Java
public static class Build.VERSION_CODES
extends Object

java.lang.Object
↳ android.os.Build.VERSION_CODES

int BASE
October 2008: The original, first, version of Android.

int BASE_1_1
February 2009: First Android update, officially called 1.1.

int CUPCAKE
May 2009: Android 1.5.

int CUR_DEVELOPMENT
Magic version number for a current development build, which has not yet turned into an official release.

int DONUT
September 2009: Android 1.6.

int ECLAIR
November 2009: Android 2.0

Applications targeting this or a later release will get these new changes in behavior:

The Service.onStartCommand function will return the new Service.START_STICKY behavior instead of the old compatibility Service.START_STICKY_COMPATIBILITY.
int ECLAIR_0_1
December 2009: Android 2.0.1

int ECLAIR_MR1
January 2010: Android 2.1

int FROYO
June 2010: Android 2.2

int GINGERBREAD
November 2010: Android 2.3

Applications targeting this or a later release will get these new changes in behavior:

The application’s notification icons will be shown on the new dark status bar background, so must be visible in this situation.
int GINGERBREAD_MR1
February 2011: Android 2.3.3.

int HONEYCOMB
February 2011: Android 3.0.

int HONEYCOMB_MR1
May 2011: Android 3.1.

int HONEYCOMB_MR2
June 2011: Android 3.2.

int ICE_CREAM_SANDWICH
October 2011: Android 4.0.

int ICE_CREAM_SANDWICH_MR1
December 2011: Android 4.0.3.

int JELLY_BEAN
June 2012: Android 4.1.

int JELLY_BEAN_MR1
November 2012: Android 4.2, Moar jelly beans!

Applications targeting this or a later release will get these new changes in behavior:

Content Providers: The default value of android:exported is now false.
int JELLY_BEAN_MR2
July 2013: Android 4.3, the revenge of the beans.

int KITKAT
October 2013: Android 4.4, KitKat, another tasty treat.

int KITKAT_WATCH
June 2014: Android 4.4W.

int LOLLIPOP
November 2014: Lollipop.

int LOLLIPOP_MR1
March 2015: Lollipop with an extra sugar coating on the outside! For more information about this release, see the Android 5.1 APIs.

int M
M is for Marshmallow!

Applications targeting this or a later release will get these new changes in behavior.

int N
N is for Nougat.

int N_MR1
N MR1: Nougat++.

int O
O.

int O_MR1
O MR1.

int P
P.

int Q
Q.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值