简体   繁体   English

Android中与联系人的权限拒绝

[英]Permission Denial with Contacts in Android

I'm trying to access the contacts and display them, but it won't let me, it denies me access...i already read others answsers in this website but still with the error...I already gave the permissions in a properly way. 我正在尝试访问并显示联系人,但它不允许我访问,它拒绝我访问...我已经在该网站上阅读了其他答案,但仍然出现错误...我已经在正确的方法。

this is the code: 这是代码:

    TextView contactView = (TextView) findViewById(R.id.contactview);
    Cursor cursor = getContacts();

    while(cursor.moveToNext()){

        String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
        contactView.append("Name: ");
        contactView.append(displayName);
        contactView.append("\n");
    }
     //End of onCreate

    private Cursor getContacts(){

    //runs the query

    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] {ContactsContract.Contacts._ID ,ContactsContract.Contacts.DISPLAY_NAME};
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'";
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    return getContentResolver().query(uri,projection,selection,selectionArgs,sortOrder);
}

This is the Manifest file: 这是清单文件:

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

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


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".ContactsActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">



        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

And the Log: 和日志:

01-17 23:12:33.250 10142-10142/com.example.pc.contentprovider E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.example.pc.contentprovider, PID: 10142                            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pc.contentprovider/com.example.pc.contentprovider.ContactsActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{5150ff8 10142:com.example.pc.contentprovider/u0a57} (pid=10142, uid=10057) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                             Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{5150ff8 10142:com.example.pc.contentprovider/u0a57} (pid=10142, uid=10057) 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.ContentResolver.query(ContentResolver.java:434)
                                                                                at com.example.pc.contentprovider.ContactsActivity.getContacts(ContactsActivity.java:60)
                                                                                at com.example.pc.contentprovider.ContactsActivity.onCreate(ContactsActivity.java:25)
                                                                                at android.app.Activity.performCreate(Activity.java:6237)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
01-17 23:13:12.640 10142-10142/? I/Process: Sending signal. PID: 10142 SIG: 9

Assuming you are using Android Studio, check your gradle.build file and see what version of Android you are targeting. 假设您使用的是Android Studio,请检查gradle.build文件,然后查看您要定位的Android版本。

If it is 23 then you need to use the new Marshmallow permissions system. 如果是23,则需要使用新的棉花糖权限系统。

If you set the TARGET to 21 and lower it will use the now older manifest declared permission system, which asks the user on App Install for all permissions at once. 如果将TARGET设置为21或更低,它将使用现在较旧的清单声明的权限系统,该系统会立即向App Install上的用户询问所有权限。 This will work on Android 6 devices too. 这也将在Android 6设备上运行。

I would encourage you to work to the former rather than the latter, as this is the newer method of requesting permissions. 我鼓励您使用前者而不是后者,因为这是请求权限的较新方法。

What version of Android is this run on? 这是在哪个版本的Android上运行? In Android 6.0 you have to request permissions in code. 在Android 6.0中,您必须请求代码权限。 You can check if this is the problem looking for this application in phone settings and giving it permissions it needs manually. 您可以检查这是否是在电话设置中查找此应用程序并手动为其赋予所需权限的问题。

With Android 6.0 the permission model has changed, now you need to ask the user for permission at Run time . 使用Android 6.0时,权限模型已更改,现在您需要在运行时向用户询问权限。 Look at this google sample which explains how to request for Contact permissions here . 查看此google示例,其中说明了如何在此处请求“联系人”权限。 Read through this developer notes here . 在此处阅读此开发人员说明。

If you are running for Android API version less than 23 then the older way which you have listed works. 如果您运行的Android API版本低于23,则列出的旧方法有效。 For anything above and equal to API version 23, you need to request Permission at Run time. 对于高于或等于API版本23的任何内容,您需要在运行时请求权限。

There are two types of permission defined from API version 23, one with PROTECTION_NORMAL and one without. API版本23定义了两种类型的权限,一种具有PROTECTION_NORMAL,另一种没有。 The PROTECTION_NORMAL permissions still work the old way example accessing network state and Internet, which do not do much Harm. PROTECTION_NORMAL权限仍然可以使用访问网络状态和Internet的旧方法示例,它们不会带来太多危害。 Check here 在这里检查

You will need to use the new permission for newer versions of android (6.0+). 您将需要为新版本的android (6.0+)使用新权限。

Here is some examples from the site: 以下是该站点的一些示例:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM