2013-02-22 84 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 }; ...

相關問題