请查找附加代码以检索来电的联系人姓名。在这里,我将数字硬编码为“123”。我已在模拟器中添加了此联系人。但是应用程序在getContentResolver()。查询时崩溃了。请帮忙。
String number = "123";
// define the columns I want the query to return
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup.NUMBER };
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// query time
Cursor c = getContentResolver().query(contactUri, projection, null,
null, null);
// if the query returns 1 or more results
// return the first result
if (c.moveToFirst()) {
String name = c.getString(c
.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
Log.i("Map", "Contact Name " + name);
}
03-16 11:12:09.304: INFO/ActivityManager(65): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=ContactName.com/.ContactName }
03-16 11:12:09.544: INFO/ActivityManager(65): Start proc ContactName.com for activity ContactName.com/.ContactName: pid=406 uid=10024 gids={}
03-16 11:12:10.284: DEBUG/ddm-heap(406): Got feature list request
03-16 11:12:11.103: ERROR/DatabaseUtils(145): Writing exception to parcel
03-16 11:12:11.103: ERROR/DatabaseUtils(145): java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/phone_lookup/9895318420 from pid=406, uid=10024 requires android.permission.READ_CONTACTS
03-16 11:12:11.103: ERROR/DatabaseUtils(145): at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:240)
03-16 11:12:11.103: ERROR/DatabaseUtils(145): at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:116)
03-16 11:12:11.103: ERROR/DatabaseUtils(145): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:98)
03-16 11:12:11.103: ERROR/DatabaseUtils(145): at android.os.Binder.execTransact(Binder.java:287)
03-16 11:12:11.103: ERROR/DatabaseUtils(145): at dalvik.system.NativeStart.run(Native Method)
03-16 11:12:11.194: DEBUG/AndroidRuntime(406): Shutting down VM
03-16 11:12:11.204: WARN/dalvikvm(406): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
03-16 11:12:11.214: ERROR/AndroidRuntime(406): Uncaught handler: thread main exiting due to uncaught exception
03-16 11:12:11.284: ERROR/AndroidRuntime(406): java.lang.RuntimeException: Unable to start activity ComponentInfo{ContactName.com/ContactName.com.ContactName}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/phone_lookup/9895318420 from pid=406, uid=10024 requires android.permission.READ_CONTACTS
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.os.Looper.loop(Looper.java:123)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at java.lang.reflect.Method.invoke(Method.java:521)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at dalvik.system.NativeStart.main(Native Method)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/phone_lookup/9895318420 from pid=406, uid=10024 requires android.permission.READ_CONTACTS
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.os.Parcel.readException(Parcel.java:1218)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.content.ContentProviderProxy.bulkQuery(ContentProviderNative.java:326)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.content.ContentProviderProxy.query(ContentProviderNative.java:345)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.content.ContentResolver.query(ContentResolver.java:202)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at ContactName.com.ContactName.onCreate(ContactName.java:31)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): ... 11 more
03-16 11:12:11.344: INFO/Process(65): Sending signal. PID: 406 SIG: 3
03-16 11:12:11.354: INFO/dalvikvm(406): threadid=7: reacting to signal 3
03-16 11:12:11.354: ERROR/dalvikvm(406): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
03-16 11:12:19.364: WARN/ActivityManager(65): Launch timeout has expired, giving up wake lock!
03-16 11:12:20.454: WARN/ActivityManager(65): Activity idle timeout for HistoryRecord{43ea9568 ContactName.com/.ContactName}
03-16 11:17:11.394: INFO/Process(406): Sending signal. PID: 406 SIG: 9
03-16 11:17:11.454: INFO/ActivityManager(65): Process ContactName.com (pid 406) has died.
03-16 11:17:11.684: ERROR/gralloc(65): [unregister] handle 0x388370 still locked (state=40000001)
03-16 11:17:11.724: INFO/UsageStats(65): Unexpected resume of com.android.launcher while already resumed in ContactName.com
03-16 11:17:11.914: WARN/InputManagerService(65): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43b7f980
03-16 11:21:00.374: DEBUG/dalvikvm(65): GC freed 13348 objects / 660920 bytes in 285ms
答案 0 :(得分:2)
ERROR/DatabaseUtils(145): java.lang.SecurityException: Permission Denial: reading
com.android.providers.contacts.ContactsProvider2 uri
content://com.android.contacts/phone_lookup/9895318420 from pid=406, uid=10024 requires
android.permission.READ_CONTACTS
解决方案正好在您发布的错误日志中。将权限READ_CONTACTS添加到您的AndroidManifest您还需要一些其他相关权限,请查看文档以查看内容。 - 您需要的将取决于您如何使用API。
答案 1 :(得分:1)
你能从logcat提供日志输出吗? 您需要一个上下文来调用getContentResolver()。您是在活动类或其他地方调用此方法吗?
编辑:您需要添加权限才能从手机的地址簿中读取联系人。请点击此链接: http://developer.android.com/reference/android/Manifest.permission.html#READ_CONTACTS