权限拒绝:在Android Studio中从ProcessRecord打开提供程序com.android.providers.contacts.ContactsProvider2

时间:2015-04-28 09:47:14

标签: android android-studio permissions contacts android-contacts

当我尝试从手机读取联系人时,我收到此错误,并且我在Manifest文件中包含READ_CONTACTS权限。奇怪的是它在 Eclipse 中运行良好,但是当我将项目转换为 Gradle 并在 Android Studio 中运行时我就是#39 ;得到这个错误。

logcat说:

  

权限拒绝:从ProcessRecord打开提供程序com.android.providers.contacts.ContactsProvider2 {302f069 29282:com.GP/u0a322}(pid = 29282,uid = 10322)需要android.permission.READ_CONTACTS或android.permission.WRITE_CONTACTS

这是清单代码:

<uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <!-- Read Contacts from phone -->
    <uses-permission android:name="android.permission.read_contacts" />
    <uses-permission android:name="android.permission.read_phone_state" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.READ_CALL_LOG" />

5 个答案:

答案 0 :(得分:47)

由于Marshmallow更新在运行时请求权限,因此读取联系人权限不起作用。示例代码是

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    // The ListView
    private ListView lstNames;

    // Request code for READ_CONTACTS. It can be any number > 0.
    private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;

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

        // Find the list view
        this.lstNames = (ListView) findViewById(R.id.lstNames);

        // Read and show the contacts
        showContacts();
    }

    /**
     * Show the contacts in the ListView.
     */
    private void showContacts() {
        // Check the SDK version and whether the permission is already granted or not.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
            //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
        } else {
            // Android version is lesser than 6.0 or the permission is already granted.
            List<String> contacts = getContactNames();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contacts);
            lstNames.setAdapter(adapter);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission is granted
                showContacts();
            } else {
                Toast.makeText(this, "Until you grant the permission, we canot display the names", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * Read the name of all the contacts.
     *
     * @return a list of names.
     */
    private List<String> getContactNames() {
        List<String> contacts = new ArrayList<>();
        // Get the ContentResolver
        ContentResolver cr = getContentResolver();
        // Get the Cursor of all the contacts
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        // Move the cursor to first. Also check whether the cursor is empty or not.
        if (cursor.moveToFirst()) {
            // Iterate through the cursor
            do {
                // Get the contacts name
                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                contacts.add(name);
            } while (cursor.moveToNext());
        }
        // Close the curosor
        cursor.close();

        return contacts;
    }
}

示例截图: enter image description here here

我从http://www.php.net/json_decode

获得了以下源代码

答案 1 :(得分:5)

我的问题是我使用的是Marshmallow模拟器,并对我们如何向用户请求权限进行了一些更改。因此,即使添加了权限,它仍然要求权限。 http://developer.android.com/training/permissions/requesting.html

答案 2 :(得分:5)

仅当应用程序的 targetSdkVersion (在清单中指定)为23或更高时,才使用新的运行时权限方案。因此,避免此问题的一种方法是使 targetSdkVersion 低于23.请参阅此处的说明:

https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous

答案 3 :(得分:0)

Google Play限制使用高风险或敏感权限,包括SMS或“通话记录”权限组。简而言之,即使您在运行时请求许可,也很有可能在Google Play中不允许您的应用。

您可以申请例外。 Google Play将视情况审查要求并授予例外。 here中提到了允许例外的条件。

答案 4 :(得分:-1)

根据有关请求许可的Google文档,当您处理它时,请牢记这一点。

例如,假设您同时在READ_CONTACTS和WRITE_CONTACTS中列出 您的应用清单。

如果您请求READ_CONTACTS并且用户授予 权限,然后您请求WRITE_CONTACTS,系统 立即授予您该权限,而无需与 用户。

https://developer.android.com/training/permissions/requesting