尝试在Android Marshmallow(Android M)中阅读联系人时Android应用崩溃

时间:2015-08-20 10:21:46

标签: android user-permissions

我已将我的应用定位到SDK版本23(Android Marsmallow)

当我尝试从我的应用程序中读取联系人时,我得到了此异常,但我在Android清单文件中提供了 READ_CONTACTS 权限。 我想简单的尝试捕获不是正确的处理。

例外详情:

  

引起:java.lang.SecurityException:Permission Denial:读取com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data from pid = 8373,uid = 10152需要android.permission。 READ_CONTACTS或grantUriPermission()

我的明确权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2 个答案:

答案 0 :(得分:9)

运行时权限

如果您需要添加的权限未列在正常权限下,则需要处理“运行时权限”。运行时权限是应用程序运行时所需的权限。这些权限将向用户显示一个对话框,类似于以下内容:

enter image description here

添加“运行时权限”的第一步是将其添加到AndroidManifest:

  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.codepath.androidpermissionsdemo" >

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    ...
</manifest>

您需要启动权限请求并处理结果。以下代码显示了如何在Activity的上下文中执行此操作,但也可以从Fragment中执行此操作。

    // MainActivity.java
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // In an actual app, you'd want to request a permission when the user performs an action
        // that requires that permission.
        getPermissionToReadUserContacts();
    }

    // Identifier for the permission request
    private static final int READ_CONTACTS_PERMISSIONS_REQUEST = 1;

    // Called when the user is performing an action which requires the app to read the
    // user's contacts
    public void getPermissionToReadUserContacts() {
        // 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid
        // checking the build version since Context.checkSelfPermission(...) is only available
        // in Marshmallow
        // 2) Always check for permission (even if permission has already been granted)
        // since the user can revoke permissions at any time through Settings
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {

            // The permission is NOT already granted.
            // Check if the user has been asked about this permission already and denied
            // it. If so, we want to give more explanation about why the permission is needed.
            if (shouldShowRequestPermissionRationale(
                    Manifest.permission.READ_CONTACTS)) {
                // Show our own UI to explain to the user why we need to read the contacts
                // before actually requesting the permission and showing the default UI
            }

            // Fire off an async request to actually get the permission
            // This will show the standard permission request dialog UI
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
                    READ_CONTACTS_PERMISSIONS_REQUEST);
        }
    }

    // Callback with the request from calling requestPermissions(...)
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String permissions[],
                                           @NonNull int[] grantResults) {
        // Make sure it's our original READ_CONTACTS request
        if (requestCode == READ_CONTACTS_PERMISSIONS_REQUEST) {
            if (grantResults.length == 1 &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Read Contacts permission granted", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Read Contacts permission denied", Toast.LENGTH_SHORT).show();
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

这里是原帖

https://guides.codepath.com/android/Understanding-App-Permissions

答案 1 :(得分:4)

这是我想要的。

http://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en

完整的权限相关解决方案。希望它会帮助某人。