权限拒绝:打开提供者com.android.providers.contacts.ContactsProvider2

时间:2015-08-23 15:27:03

标签: android android-studio login

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

<?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>

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

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)

导致此异常的原因是什么?

2 个答案:

答案 0 :(得分:7)

如果您在Android M上进行测试,则必须要求用户在运行时阅读联系人,如下所示:

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.
        }
    }
}

将此添加到您的导入中:

      import static android.Manifest.permission.READ_CONTACTS;

并将一个整数定义为Id以识别READ_CONTACTS权限请求。

      private static final int REQUEST_READ_CONTACTS = 0;
希望这会有所帮助。

答案 1 :(得分:-8)

Api 23有一些安全性改进(这意味着开发的一些错误)。我确定你定义了targetSdkVersion="23"。如果你将它改为22,它将正常工作,检查