Android dev:READ_CONTACTS时的SecurityException

时间:2015-03-08 09:53:52

标签: android android-contacts android-permissions

我是Android开发的新手。以下是我尝试访问联系人时获得的内容。 调试日志:

... ...
03-08 17:30:39.622    2330-2330/com.learning.zhanggu.test06 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.learning.zhanggu.test06, PID: 2330
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.learning.zhanggu.test06/com.learning.zhanggu.test06.Test06}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{25e387d9 2330:com.learning.zhanggu.test06/u0a58} (pid=2330, uid=10058) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
... ...
     Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{25e387d9 2330:com.learning.zhanggu.test06/u0a58} (pid=2330, uid=10058) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
... ...
            at android.content.ContentResolver.query(ContentResolver.java:422)
            at com.learning.zhanggu.test06.Test06.fetchContacts(Test06.java:48)
            at com.learning.zhanggu.test06.Test06.onCreate(Test06.java:20)
... ...

我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.learning.zhanggu.test06" >
    <uses-sdk android:minSdkVersion="8" />
    <user-permission android:name="android.permission.READ_CONTACTS" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Test06"
            android:label="@string/title_activity_test06" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

我的Java文件:

    public void fetchContacts() {
        StringBuffer output = new StringBuffer();
        Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (cursor.getCount() >0) {
            while (cursor.moveToNext()) {
                String name = getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                output.append("Name: "+name+"\n");
            }
            textView1.setText(output);
        }
        cursor.close();
    }

环境: Windows 8.1上的Android Studio 1.1.0 模拟器:Nexus_4_API_19,Nexus_5_API_21

我搜索了一下网页并尝试将权限放入活动注册但到目前为止没有运气。

愿任何人友好地建议,非常感谢你。

3 个答案:

答案 0 :(得分:3)

它应该是use-permission,而不是用户

答案 1 :(得分:0)

这是我的区分大小写问题。下面的代码对我有用。

mvn install

答案 2 :(得分:0)

替换

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

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

它应该<uses ...而不是<user ..

试试这个.... :)