尝试在 Android Marshmallow (Android M) 中读取联系人时,Android 应用程序崩溃

标签 android user-permissions

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

当我尝试从我的应用程序中读取联系人时,我遇到了这个异常,尽管我在我的 Android list 文件中授予了 READ_CONTACTS 权限。 我想简单的 try catch 不是正确的处理方式。

异常详情:

Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data from pid=8373, uid=10152 requires android.permission.READ_CONTACTS, or grantUriPermission()

我的 list 权限:

<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"/>

最佳答案

运行时权限

如果您需要添加的权限没有列在普通权限下,您需要处理“运行时权限”。运行时权限是在应用程序运行时根据需要请求的权限。这些权限将向用户显示一个对话框,类似于以下对话框:

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

关于尝试在 Android Marshmallow (Android M) 中读取联系人时,Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32115650/

相关文章:

ArrayList 上出现 java.lang.IndexOutOfBoundsException

android - 使用 Dagger 2,将 Activity 注入(inject)其本地组件是否正确?

java - 如何使连拍模式可用于相机

grails - Grails中的用户访问控制

laravel - 阻止用户查看其他用户正在查看的内容的最佳方法?

android - 接受带有自签名证书的 HTTPS 连接

android - Facebook - SingleInstance Activity 上的 LoginFragment

ios - React Native navigator.geolocation.getCurrentPosition 如何自定义 Alert Text Message

django - 用于服务媒体的用户权限 Django

ios - 如果健康应用程序尚未设置,如何使用 healthkit?