为什么在询问用户后权限被拒绝?

时间:2016-05-18 22:33:41

标签: android permissions

我在我的manisfest中拥有api 22的权限 - :

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission
    android:name="android.permission.ACCESS_FINE_LOCATION"
    android:maxSdkVersion="22" />
<uses-permission
    android:name="android.permission.ACCESS_COARSE_LOCATION"
    android:maxSdkVersion="22" />
<uses-permission
    android:name="android.permission.READ_CONTACTS"
    android:maxSdkVersion="22" />
<uses-permission
    android:name="android.permission.WRITE_CONTACTS"
    android:maxSdkVersion="22" />
<uses-permission
    android:name="android.permission.READ_EXTERNAL_STORAGE"
    android:maxSdkVersion="22" />

运行时检查权限以及api 23 +:

private void requestGpsPermission() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_CONTACTS)
            != PackageManager.PERMISSION_GRANTED ||
            ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_CONTACTS)
            != PackageManager.PERMISSION_GRANTED)
        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.READ_CONTACTS,
                Manifest.permission.WRITE_CONTACTS}, 1);

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED ||
            ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_COARSE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED)
        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION}, 2);

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED)
        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.READ_EXTERNAL_STORAGE}, 3);
}

我仍然有错误:

 Writing exception to parcel
 java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/fs_id from pid=20520, uid=10089 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
                                                      at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:605)
                                                      at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:480)
                                                      at android.content.ContentProvider$Transport.query(ContentProvider.java:211)
                                                      at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
                                                      at android.os.Binder.execTransact(Binder.java:453)

Failed to clear out contacts
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{9b0bd73 20520:com.google.android.apps.plus/u0a89} (pid=20520, uid=10089) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
                                                                       at android.os.Parcel.readException(Parcel.java:1620)
                                                                       at android.os.Parcel.readException(Parcel.java:1573)
                                                                       at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3594)
                                                                       at android.app.ActivityThread.acquireProvider(ActivityThread.java:4799)
                                                                       at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2018)
                                                                       at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1466)
                                                                       at android.content.ContentResolver.query(ContentResolver.java:475)
                                                                       at android.content.ContentResolver.query(ContentResolver.java:434)
                                                                       at ddt.a(PG:447)
                                                                       at ddt.b(PG:1396)
                                                                       at ddu.run(PG:333)
                                                                       at java.lang.Thread.run(Thread.java:818)

我正在尝试在Android 6.0设备中运行该应用。关于如何解决它的任何想法?

1 个答案:

答案 0 :(得分:0)

经过2天的努力,我终于成功了。我错过了以下方法:

1 @Override
2 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
3     // If request is cancelled, the result arrays are empty.
4     if (grantResults.length > 0
5             && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
6         // permission granteed
7        P.toast(this, "permission granteed :-)");
8         getHeaders();
9     } else {
10        // permission denied
11        P.toast(this, "permission denied :-(");
12    }
13    return;
14}

我也必须修改验证方法,所以如果我已经拥有权限它从那里调用getHeaders():

1 private void requestGpsPermission() {
2     if (
3             ContextCompat.checkSelfPermission(this,
4                     Manifest.permission.READ_CONTACTS)
5                     != PackageManager.PERMISSION_GRANTED ||
6                     ContextCompat.checkSelfPermission(this,
7                             Manifest.permission.ACCESS_FINE_LOCATION)
8                             != PackageManager.PERMISSION_GRANTED ||
9                     ContextCompat.checkSelfPermission(this,
10                            Manifest.permission.READ_EXTERNAL_STORAGE)
11                            != PackageManager.PERMISSION_GRANTED
12            ) {
13        ActivityCompat.requestPermissions(this, new String[]{
14                Manifest.permission.READ_CONTACTS,
15                Manifest.permission.ACCESS_FINE_LOCATION,
16                Manifest.permission.READ_EXTERNAL_STORAGE
17        }, 1);
18    } else {
19        getHeaders();
20    }
21}

请注意,我在第一个mehotd的第8行和第二个mehotd的第19行调用相同的方法。