如何访问我的 Android 程序中的联系人
我正在制作一个短信应用程序,并且想要访问我的 Android 应用程序中的联系人 。
我想访问联系人,就像他们在实际联系人列表中一样。选择后,我需要返回到我的活动,在其中我可以向该人发送短信。或者是否可以访问存储联系人的数据库?
我的代码如下所示:
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String no = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
我的 LogCat 如下所示:
09-07 12:46:15.458: DEBUG/AndroidRuntime(473): Shutting down VM
09-07 12:46:15.458: WARN/dalvikvm(473): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): FATAL EXCEPTION: main
09-07 12:46:15.478: ERROR/AndroidRuntime(473): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-512D3F3D533B/1 flg=0x1 (has extras) }} to activity {task.list/task.list.Msgactivity}: java.lang.IllegalArgumentException: column 'data1' does not exist
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.os.Looper.loop(Looper.java:123)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at java.lang.reflect.Method.invoke(Method.java:507)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at dalvik.system.NativeStart.main(Native Method)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): Caused by: java.lang.IllegalArgumentException: column 'data1' does not exist
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:99)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at task.list.Msgactivity.onActivityResult(Msgactivity.java:99)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): ... 11 more
09-07 12:46:15.489: WARN/ActivityManager(61): Force finishing activity task.list/.Msgactivity
谁能帮助我?
I'm making an text message application and want to access contacts in my Android application
.
I want to access contacts as if they where in the actual contact list. When selected I need to return to my activity in which I can send an SMS to that person. Or is it possible to access the database in which contacts are stored?
My code is shown below:
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String no = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
My LogCat looks like this:
09-07 12:46:15.458: DEBUG/AndroidRuntime(473): Shutting down VM
09-07 12:46:15.458: WARN/dalvikvm(473): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): FATAL EXCEPTION: main
09-07 12:46:15.478: ERROR/AndroidRuntime(473): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-512D3F3D533B/1 flg=0x1 (has extras) }} to activity {task.list/task.list.Msgactivity}: java.lang.IllegalArgumentException: column 'data1' does not exist
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.os.Looper.loop(Looper.java:123)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at java.lang.reflect.Method.invoke(Method.java:507)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at dalvik.system.NativeStart.main(Native Method)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): Caused by: java.lang.IllegalArgumentException: column 'data1' does not exist
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:99)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at task.list.Msgactivity.onActivityResult(Msgactivity.java:99)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): ... 11 more
09-07 12:46:15.489: WARN/ActivityManager(61): Force finishing activity task.list/.Msgactivity
Can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用此代码选择联系人:
use this code to pick contact:
检查以下链接:
如何调用 Android 联系人列表?
如何从android中的本机电话簿获取联系人
如何在 Android 中获取联系人的所有详细信息
如何从 Android 获取名字和姓氏联系人?
如何将联系人从电话簿导入到我们的应用程序
Android 联系人提取
如何获取所有Android联系人但没有SIM 卡上的内容
http://mobile.tutsplus.com/tutorials/android/android-essentials-using-the-contact-picker/
check these links:
How to call Android contacts list?
How to get contacts from native phonebook in android
How to obtain all details of a contact in Android
How to get the first name and last name from Android contacts?
How to import contacts from phonebook to our application
Android contacts extraction
How to get all android contacts but without those which are on SIM
http://mobile.tutsplus.com/tutorials/android/android-essentials-using-the-contact-picker/
试试这个代码。 这会将所有联系人存储在 ArrayList 中。
如果您想存储特定联系人,请使用 if 条件,
try this code. this stores all the contacts in the ArrayList
if you want to store the specific contact use the if condition.
我可以使用 @Balaji.K 的示例进行一些修改。我遇到的主要问题是
会跳过第一个结果。
我还想建立一个电话号码列表,如果没有电话号码被标记为“移动”,用户可以从中进行选择。我还传递了一个名称进行搜索(在本例中替换为变量),但是可以修改此代码以通过将“selection”和“selection_args”替换为 null 并迭代“contacts”中的所有内容来构建一个集合,就像我为“电话”。这是我最终得到的结果(在 Kotlin 中):
I was able to use @Balaji.K 's example with some modification. The main issue I ran into is that
will skip over the first result.
I also wanted to build a list of phone numbers for a user to select from if none are marked as "Mobile". I also pass in a name to search (variable replaced with in this example), but one could modify this code to build a collection by replacing "selection" and "selection_args" with null and iterating over everything in "contacts" as I did for "phones". Here is what I ended up with (in Kotlin):