2013-02-22 35 views
0

我想从联系人中检索手机号码(我想发送短信)。在这里我的代码:从联系人中检索手机号码

//Selecting the contact 
     Button buttonPickContact = (Button)findViewById(R.id.pickcontact); 
     buttonPickContact.setOnClickListener(new Button.OnClickListener(){ 

      public void onClick(View v) { 
       Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
       startActivityForResult(intent, RQS_PICK_CONTACT); 
      }}); 


@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 

     Cursor cursor = null; 
     mName.setText(context.getString(R.string.not_available)); 
     mNumber.setText(context.getString(R.string.not_available)); 

     if(requestCode == RQS_PICK_CONTACT && resultCode == RESULT_OK && data != null){ 
      Log.d(TAG, "requestCode, resultCode, data ok"); 
      Uri uri = data.getData(); 
      try{ 
       String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER}; 
//    cursor = getContentResolver().query(uri, projection, null, null, null); 
       cursor = getContentResolver().query(uri, null, null, null, null); 
       cursor.moveToFirst(); 
       Log.d(TAG, "Trying to retrieve the name and the number"); 
       String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
       String hasNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER)); 

       Log.d(TAG, "hasNumber "+hasNumber); 
       mName.setText(name); 

       if(hasNumber.trim().equals("1")){ 
        Log.d(TAG, "contact has telephone number"); 
        //set name and number 
        String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        mNumber.setText(phoneNumber); 
       } 

      }catch(Exception ex){ 
       CharSequence text = context.getString(R.string.cannot_choose_contact); 
       Toast toast = Toast.makeText(context, text, duration); 
       toast.show(); 
      } 
      if(cursor!= null && !cursor.isClosed()){ 
       cursor.close(); 
      } 
     }else{ 
      CharSequence text = context.getString(R.string.cannot_choose_contact); 
      Toast toast = Toast.makeText(context, text, duration); 
      toast.show(); 
     } 
    } 

我得到:无法读取行0,列-1从CursorWindow ...

我如何获得的电话号码 - 我在试图从右侧检索柱?

预先感谢您的回答,

回答

1

联系人的详细数据包含在一个单独的表从主接触本身(见Contacts API guide更多细节)。由于您正在发送短信,因此只有获得关联电话号码的联系人可能会更有用,因此您最好直接查找包含电话号码的表格。对于URI,我使用:

CommonDataKinds.Phone.CONTENT_URI 

那么你不必担心HAS_PHONE_NUMBER。一目了然,其余的代码看起来正确或非常接近。如果您想继续使用原始路径,则必须在该表上单独进行查询,但要为其提供最初找到的联系人的ID。

1
  1. 使用CursorLoader进行查询。总是。如果你继续在UI线程上查询,最终你会遇到挂起系统并获得ANR的情况。
  2. 您要求用户从联系人表中选择联系人,以便您找回指向联系人中联系人的URI。
  3. 处理这个问题的一个窍门是使用 Uri.getLastPathSegment()从联系人的LOOKUP_KEY中去掉返回的URI。然后为LOOKUP_KEY和 MIMETYPE值CommonDataKinds.Email.CONTENT_ITEM_TYPE搜索ContactsContract.Data。根据您的代码,这将是:

mLookupKey = uri.getLastPathSegment(); String SELECTION = Data.LOOKUP_KEY +“=?”+ “AND”+ Data.MIMETYPE +“=?”; String [] selectionArgs = { mLookupKey, Email.CONTENT_ITEM_TYPE }; ...

相关问题