2017-04-24 67 views
0

我試圖從我的應用程序中讀取聯繫人詳細信息,此刻只是登錄到控制檯,但在模擬器和我自己的設備上進行測試時遇到安全異常。我正在按照本教程作爲參考https://www.youtube.com/watch?v=g4_1UOFNLEY閱讀聯繫人Securty異常錯誤

如果任何人都可以幫助我或指出我在訪問聯繫人的更好方式的正確方向,將不勝感激。提前致謝。

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)  

回答

1

您需要persmissions已經運行實時讀取/寫入聯繫人的權限f ROM與API設備23和更高:

if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { 

     if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)) { 
       ActivityCompat.requestPermissions(this, 
         new String[]{Manifest.permission.READ_CONTACTS}, 
         MY_PERMISSIONS_REQUEST_READ_CONTACTS); 
     }else{ 
       ActivityCompat.requestPermissions(this, 
         new String[]{Manifest.permission.READ_CONTACTS}, 
         MY_PERMISSIONS_REQUEST_READ_CONTACTS); 
      } 
     } else { 
      //do your work here 
     } 

而且讀取狀態:

@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(); 
       } 
      } 
     } 
    } 

您可以在Developer Site

+0

感謝Sanjog,感謝我將嘗試這個爲我的應用程序現在 –

+0

讓我知道這是否會幫助你。 –

+0

獲取一些錯誤仍然如果你可以協助,我得到這個錯誤 java.lang.RuntimeException:無法啓動活動ComponentInfo {應用程序/ android.database.CursorIndexOutOfBoundsException:索引2請求,大小爲2 –