阅读联系人Securty异常错误

时间:2017-04-24 13:56:13

标签: android

我正在尝试从我的应用程序中读取联系人详细信息,目前只是登录到控制台但我在模拟器和我自己的设备上进行测试时遇到安全异常。我关注本教程作为参考https://www.youtube.com/watch?v=g4_1UOFNLEY

如果有人可以帮助我,或者指出我在更好的方式访问联系人的正确方向,我将不胜感激。提前谢谢。

    ContentResolver resolver = getContentResolver();
    // to gets all contacs
    Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    while(cursor.moveToNext()){
        // Access specific field of contact
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        //get phone numbers
        Cursor phoneCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{ contactId }, null);

        Log.i("MY INFO", contactId + " = " +contactName);
        //get phone num
        while(phoneCursor.moveToNext()){
            String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            Log.i("MY INFO", phoneNumber);
        }
        // get email details
        Cursor emailCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",new String[]{ contactId}, null);

        while(emailCursor.moveToNext()){
            String contactEmail = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

            Log.i("MY INFO", contactEmail);

        }
    }

的AndroidManifest.xml

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

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

        <activity android:name=".SignInActivity" />
        <activity android:name=".EditPlayersActivity"/>

    </application>

</manifest>

错误

UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.HtcContactsProvider2 from ProcessRecord{/u0a275} (pid=7241, uid=10275) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2484)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
                                                                            at android.app.ActivityThread.access$900(ActivityThread.java:150)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:168)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5845)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
                                                                         Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.HtcContactsProvider2 from ProcessRecord{c71ad8f 7241:com.ciaranbyrne.squad/u0a275} (pid=7241, uid=10275) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
                                                                            at android.os.Parcel.readException(Parcel.java:1608)
                                                                            at android.os.Parcel.readException(Parcel.java:1561)
                                                                            at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3598)
                                                                            at android.app.ActivityThread.acquireProvider(ActivityThread.java:5137)
                                                                            at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2026)
                                                                            at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1481)
                                                                            at android.content.ContentResolver.query(ContentResolver.java:488)
                                                                            at android.content.ContentResolver.query(ContentResolver.java:447)
                                                                            at com.ciaranbyrne.squad.MainActivity.onCreate(MainActivity.java:66)
                                                                            at android.app.Activity.performCreate(Activity.java:6248)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544) 
                                                                            at android.app.ActivityThread.access$900(ActivityThread.java:150) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:168) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5845) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 
04-24 14:39:39.461 7241-7241/com.ciaranbyrne.squad E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.ciaranbyrne.squad, PID: 7241
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ciaranbyrne.squad/com.ciaranbyrne.squad.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.HtcContactsProvider2 from ProcessRecord{c71ad8f 7241:com.ciaranbyrne.squad/u0a275} (pid=7241, uid=10275) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2484)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
                                                                         at android.app.ActivityThread.access$900(ActivityThread.java:150)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:168)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5845)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
                                                                      Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.HtcContactsProvider2 from ProcessRecord{c71ad8f 7241:com.ciaranbyrne.squad/u0a275} (pid=7241, uid=10275) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
                                                                         at android.os.Parcel.readException(Parcel.java:1608)
                                                                         at android.os.Parcel.readException(Parcel.java:1561)
                                                                         at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3598)
                                                                         at android.app.ActivityThread.acquireProvider(ActivityThread.java:5137)
                                                                         at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2026)
                                                                         at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1481)
                                                                         at android.content.ContentResolver.query(ContentResolver.java:488)
                                                                         at android.content.ContentResolver.query(ContentResolver.java:447)
                                                                         at com.ciaranbyrne.squad.MainActivity.onCreate(MainActivity.java:66)
                                                                         at android.app.Activity.performCreate(Activity.java:6248)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544) 
                                                                         at android.app.ActivityThread.access$900(ActivityThread.java:150) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:168) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5845) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 

3 个答案:

答案 0 :(得分:1)

在最新版本的Android中,您应该在运行时获得持久性 https://developer.android.com/training/permissions/requesting.html

答案 1 :(得分:1)

您需要拥有运行时权限才能使用 API 23 及更高版本从设备读取/写入联系人:

 @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Do your work here
                } else {
                  Snackbar.make(mToolbar,getResources().getString(R.string.no_read_contact_permission_message),
                            Snackbar.LENGTH_SHORT).show();
                }
            }
        }
    }

阅读状态:

{{1}}

您可以在Developer Site

中详细了解相关信息

答案 2 :(得分:0)

如果您拥有接受API 23级或高级级别的设备,则需要具有运行时权限才能读取/写入联系人:

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


And you can call in manifest also:<uses-permission android:name="android.permission.READ_CONTACTS" />