我試圖從我的應用程序中讀取聯繫人詳細信息,此刻只是登錄到控制檯,但在模擬器和我自己的設備上進行測試時遇到安全異常。我正在按照本教程作爲參考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)
感謝Sanjog,感謝我將嘗試這個爲我的應用程序現在 –
讓我知道這是否會幫助你。 –
獲取一些錯誤仍然如果你可以協助,我得到這個錯誤 java.lang.RuntimeException:無法啓動活動ComponentInfo {應用程序/ android.database.CursorIndexOutOfBoundsException:索引2請求,大小爲2 –