电话号码检索意图

Posted

技术标签:

【中文标题】电话号码检索意图【英文标题】:Phone number retrieve intent 【发布时间】:2013-06-18 14:11:43 【问题描述】:

我正在使用意图检索电话号码。它正在显示联系人列表。现在我想找一个联系人。但是当我点击任何一个联系人时,出现异常。它发生在号码检索期间,但对于联系人姓名检索是正确的。

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, 2); 


Uri contactUri = data.getData(); 
        //String[] projection = Phone.NUMBER;

              Cursor cursor = getContentResolver()
                .query(contactUri, null, null, null, null);
              cursor.moveToNext();

             // String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));// name got me.
              String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); // exception for phone number.

              System.out.println("name "+name+phone);

logcat:-

 06-21 18:06:38.394: E/androidRuntime(30700): java.lang.RuntimeException: Failure delivering      result ResultInfowho=null, request=2, result=-1, data=Intent     dat=content://com.android.contacts/contacts/lookup/1135r4-2727494D372D37.3754r14-2727494D372D37/4     to activity com.MednymaSecureChat/com.MednymaSecureChat.XMPPClient: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.app.ActivityThread.access$1100(ActivityThread.java:123)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.os.Looper.loop(Looper.java:137)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.app.ActivityThread.main(ActivityThread.java:4424)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at java.lang.reflect.Method.invokeNative(Native Method)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at java.lang.reflect.Method.invoke(Method.java:511)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at dalvik.system.NativeStart.main(Native Method)
06-21 18:06:38.394: E/AndroidRuntime(30700): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.database.CursorWindow.nativeGetString(Native Method)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.database.CursorWindow.getString(CursorWindow.java:442)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.database.CursorWrapper.getString(CursorWrapper.java:114)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at com.MednymaSecureChat.XMPPClient.onActivityResult(XMPPClient.java:480)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.app.Activity.dispatchActivityResult(Activity.java:4654)
06-21 18:06:38.394: E/AndroidRuntime(30700):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
06-21 18:06:38.394: E/AndroidRuntime(30700):    ... 11 more

请任何人帮助我

【问题讨论】:

你能发布你的 logcat 异常吗? 您要选择联系电话和姓名吗? 是的。我想两个都选。 我更新了我的答案,如果有任何问题请告诉我! 我不认为你会从选择器那里得到号码。您需要读取联系人 ID,然后使用另一个光标检索号码 【参考方案1】:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  
startActivityForResult(intent, 1);  

// ...

@Override  
public void onActivityResult(int reqCode, int resultCode, Intent data)   
    super.onActivityResult(reqCode, resultCode, data);  
    if (resultCode == Activity.RESULT_OK)   
        Uri contactData = data.getData();  
        Cursor c =  managedQuery(contactData, null, null, null, null);  
        if (c.moveToFirst())   
            String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));  
           Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
  ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
    phones.moveToFirst();
    String phone= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      tv.setText(name+" "+phone);
          
      

【讨论】:

没有库 给我电话号码而不是号码 列data1不存在数字异常 现在由***.com/questions/7330165/…工作【参考方案2】:

试试这个代码

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    
        if (requestCode == 2)
        
         Uri contactUri = data.getData(); 
        //String[] projection = Phone.NUMBER;

              Cursor cursor = getContentResolver()
                .query(contactUri, null, null, null, null);
              cursor.moveToNext();

             // String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));// name got me.
              String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); // exception for phone number.

              System.out.println("name "+name+phone);
    
  

(或) 试试下面的链接堆栈帖子,选择联系人号码并命名它对我有用

Pick a Number and Name From Contacts List in android app

【讨论】:

请注意,不鼓励仅链接的答案(链接会随着时间的推移变得陈旧)。请考虑在此处编辑您的答案并添加概要。

以上是关于电话号码检索意图的主要内容,如果未能解决你的问题,请参考以下文章

在 startActivityForResult() 之后检索联系人姓名和电话 [重复]

如何通过 LookUpKey 获取联系电话号码?

通过具有多个电话号码的意图添加新联系人

联系人列表仅使用意图 ContactsContract 显示一个电话号码

从联系人选择器意图中选择电子邮件、姓名和电话号码

在 Android 设备中使用 ACTION_PICK 意图仅显示带有电话号码的联系人