android,令人沮丧的权限

时间:2015-12-04 18:32:10

标签: android permissions

最后几天我真的很沮丧,因为我常常得到一些权限异常,说明我没有在清单中定义正确的权限。 例如,我正在尝试运行一些代码:

ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(uri, new String[]{ContactsContract.Contacts._ID}, null, null, null);
        if(cursor.moveToFirst()) {
            Long contactId = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID));

            Cursor cursorPhones = contentResolver.query(Phone.CONTENT_URI,null,Phone.CONTACT_ID+"="+contactId,null,null);

        }

第一个查询成功,而第二个查询给我异常,这里是堆栈跟踪:

12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime: FATAL EXCEPTION: main
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime: Process: com.example.pablo.appcontacts, PID: 4597
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-472955313F/1 flg=0x1 }} to activity {com.example.pablo.appcontacts/com.example.pablo.appcontacts.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=4597, uid=10061 requires android.permission.READ_CONTACTS, or grantUriPermission()
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.app.ActivityThread.-wrap16(ActivityThread.java)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:  Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=4597, uid=10061 requires android.permission.READ_CONTACTS, or grantUriPermission()
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1599)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.content.ContentResolver.query(ContentResolver.java:491)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.content.ContentResolver.query(ContentResolver.java:434)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at com.example.pablo.appcontacts.MainActivity.getPhoneNumber(MainActivity.java:93)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at com.example.pablo.appcontacts.MainActivity.renderContact(MainActivity.java:69)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at com.example.pablo.appcontacts.MainActivity.onActivityResult(MainActivity.java:57)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.app.Activity.dispatchActivityResult(Activity.java:6428)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.app.ActivityThread.-wrap16(ActivityThread.java) 
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
12-04 18:25:43.689 4597-4597/com.example.pablo.appcontacts E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
12-04 18:25:50.333 4597-4606/com.example.pablo.appcontacts W/CursorWrapperInner: Cursor finalized without prior close()
12-04 18:26:32.809 4597-4604/com.example.pablo.appcontacts W/art: Suspending all threads took: 14.922ms
12-04 18:28:27.460 4597-4604/com.example.pablo.appcontacts W/art: Suspending all threads took: 14.635ms

有没有人经历过类似的麻烦?在尝试阅读短信时,我有那些服务和内部广播接收器...

修改

这是清单:

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

    <uses-permission android:name="android.permission.READ_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=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

目标SDK是API 23:Android 6.0(Marshmallow),min Sdk是15:Android 4.0.3。 模拟器在API 23:Android 6.0上运行。

0 个答案:

没有答案