本文介绍了权限拒绝:打开提供商 com.android.providers.contacts.ContactsProvider2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 新手,我使用 Android Studio 和 LoginActivity 模板从头开始创建了一个应用程序.我的目标是 SDK 23,最低版本是 15.Android Studio 生成了以下清单:

I'm new to Android and I created an app from scratch using Android Studio and the LoginActivity template. I'm targeting SDK 23 and the min version is 15. Android Studio generated the following manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.uprope.uprope" >

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustResize|stateVisible" >
        </activity>
    </application>

</manifest>

当我尝试运行空模板时,我得到了这个堆栈跟踪:

When I try to run the empty template, I get this stack trace:

E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.uprope.uprope, PID: 24790
    java.lang.RuntimeException: An error occurred while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:309)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{85980a6 24790:com.uprope.uprope/u0a58} (pid=24790, uid=10058) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
            at android.os.Parcel.readException(Parcel.java:1599)
            at android.os.Parcel.readException(Parcel.java:1552)
            at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3550)
            at android.app.ActivityThread.acquireProvider(ActivityThread.java:4778)
            at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2018)
            at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1468)
            at android.content.ContentResolver.query(ContentResolver.java:475)
            at android.content.CursorLoader.loadInBackground(CursorLoader.java:64)
            at android.content.CursorLoader.loadInBackground(CursorLoader.java:56)
            at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
            at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
            at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:66)
            at android.os.AsyncTask$2.call(AsyncTask.java:295)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
            at java.lang.Thread.run(Thread.java:818)

是什么导致了这个异常?

What is causing this exception?

推荐答案

如果您在 android M 上进行测试,您必须像这样请求用户在运行时读取联系人的权限:

If you are testing on android M you must ask the user for permission to read contacts at run time like this:

private void accessContacts(){
    if (!mayRequestContacts()) {
        return;
    }
    // This Build is < 6 , you can Access contacts here.
}

您请求这样的许可:

private boolean mayRequestContacts() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return true;
    }
    if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
        return true;
    }
    if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {
        Snackbar.make(mEmailView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)
                .setAction(android.R.string.ok, new View.OnClickListener() {
                    @Override
                    @TargetApi(Build.VERSION_CODES.M)
                    public void onClick(View v) {
                        requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
                    }
                });
    } else {
        requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
    }
    return false;
}

然后覆盖onRequestPermissionsResult

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    if (requestCode == REQUEST_READ_CONTACTS) {
        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission granted , Access contacts here or do whatever you need.
        }
    }
}

将此添加到您的导入中:

Add this to your imports:

      import static android.Manifest.permission.READ_CONTACTS;

并定义一个整数作为 Id 来标识 READ_CONTACTS 权限请求.

and define an integer as an Id to identity READ_CONTACTS permission request.

      private static final int REQUEST_READ_CONTACTS = 0;

希望这有帮助.

这篇关于权限拒绝:打开提供商 com.android.providers.contacts.ContactsProvider2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:52