2013-05-06 202 views
0

我正在开发一个应用程序,其中我正在使用startactivity结果并访问我的应用程序中的默认android电话簿,之后选择一个联系人,获取姓名,所选联系人的一个电话号码。我想要检索多个电话号码,如果任何联系人有它和类型的电话号码,如工作,移动等请帮助我在这,任何帮助,将不胜感激。从选定的联系人获取多个电话号码

回答

1

试试这个,因为这对我的作品:

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

然后在onActivityResult执行以下操作:

Cursor cursor = null; 
      String phoneNumber = "", primaryMobile = ""; 

      List<String> allNumbers = new ArrayList<String>(); 
      int contactIdColumnId = 0, phoneColumnID = 0, nameColumnID = 0; 
      try { 
       Uri result = data.getData(); 
       Utils.printLog(TAG, result.toString()); 
       String id = result.getLastPathSegment(); 
       cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null); 
       contactIdColumnId = cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID); 
       phoneColumnID = cursor.getColumnIndex(Phone.DATA); 
       nameColumnID = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); 

       if (cursor.moveToFirst()) { 
        while (cursor.isAfterLast() == false) { 
         idContactBook = cursor.getString(contactIdColumnId); 
         displayName = cursor.getString(nameColumnID); 
         phoneNumber = cursor.getString(phoneColumnID); 

         if (phoneNumber.length() == 0) 
          continue; 

         int type = cursor.getInt(cursor.getColumnIndex(Phone.TYPE)); 
         if (type == Phone.TYPE_MOBILE && primaryMobile.equals("")) 
          primaryMobile = phoneNumber; 
         allNumbers.add(phoneNumber); 
         cursor.moveToNext(); 
        } 
       } else { 
        // no results actions 
       } 
      } catch (Exception e) { 
       // error actions 
      } finally { 
       if (cursor != null) { 
        cursor.close(); 
       } 
} 
+0

通过给这个Phone.TYPE_MOBILE我们可以得到只有手机类型的电话号码有没有任何方法可以让我获得与一个联系人相关的所有电话号码。感谢您的帮助 – Ullas 2013-05-06 12:18:13

+0

为此,你需要添加一个开关case语句或其他如例如: 开关(类型){ 情况Phone.TYPE_MOBILE: 情况下Phone.TYPE_WORK: 情况下Phone.TYPE_HOME: } – 2013-05-06 12:36:52

+0

你能为我举个例子吗? – Ullas 2013-05-06 12:38:43

0

试试这个

Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId); 
Uri phonesUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY); 
String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL} 
Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null); 
相关问题