2012-01-08 88 views
7

我想从通话记录中获取联系人。我可以使用此代码得到主触头的联络号码:Android - 如何从通话记录中获得联系人?

public void getContacts(View view) { 

    Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
    startActivityForResult(intentContact, 0); 

} 

public void onActivityResult(int requestCode, int resultCode, Intent intent) 
{ 

    if (requestCode == 0) 
    { 
     try { 
     to.setText(getContactInfo(intent)); 
     } catch(NullPointerException e) { 
       // Do nothing ;) 
     } 

    } 

} 
protected String getContactInfo(Intent intent) 
{ 
    String phoneNumber = to.getText().toString(); 
    Cursor cursor = managedQuery(intent.getData(), null, null, null, null); 
    while (cursor.moveToNext()) 
    { 
     String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
     String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
     if(phoneNumber.endsWith(">")) 
      phoneNumber += ", "+name; 
     else 
     phoneNumber += name; 
     String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 

     if (hasPhone.equalsIgnoreCase("1")) 
      hasPhone = "true"; 
     else 
      hasPhone = "false" ; 

     if (Boolean.parseBoolean(hasPhone)) 


     { 
      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); 
      while (phones.moveToNext()) 
      { phoneNumber = phoneNumber + " <" + phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))+">"; 


       } 
      phones.close(); 
     } 


    } 
    cursor.close(); 
    return phoneNumber; 
} 

这里做的事情是,当我们点击“联系人”按钮,它打开一个列表的所有联系人,用户可以选择任何接触和选择联系人将被添加到“收件人”字段中。我想要做的完全一样的事情,而不是显示所有联系人,我只想显示那些最近使用过的(通话记录)进行选择。

如果你能告诉如何在团体中做到这一点也是很好的。

回答

2

我得到这个将使用我自己的版本。我使用了一个对话框,并将光标移交给通话记录。这里是功能:

public void getCallLog() { 

    String[] callLogFields = { android.provider.CallLog.Calls._ID, 
      android.provider.CallLog.Calls.NUMBER, 
      android.provider.CallLog.Calls.CACHED_NAME /* im not using the name but you can*/}; 
    String viaOrder = android.provider.CallLog.Calls.DATE + " DESC"; 
    String WHERE = android.provider.CallLog.Calls.NUMBER + " >0"; /*filter out private/unknown numbers */ 

    final Cursor callLog_cursor = getActivity().getContentResolver().query(
      android.provider.CallLog.Calls.CONTENT_URI, callLogFields, 
      WHERE, null, viaOrder); 

    AlertDialog.Builder myversionOfCallLog = new AlertDialog.Builder(
      getActivity()); 

    android.content.DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialogInterface, int item) { 
      callLog_cursor.moveToPosition(item); 

      Log.v("number", callLog_cursor.getString(callLog_cursor 
        .getColumnIndex(android.provider.CallLog.Calls.NUMBER))); 

      callLog_cursor.close(); 

     } 
    }; 
    myversionOfCallLog.setCursor(callLog_cursor, listener, 
      android.provider.CallLog.Calls.NUMBER); 
    myversionOfCallLog.setTitle("Choose from Call Log"); 
    myversionOfCallLog.create().show(); 
} 
0

您可以使用ContactsContract.Contacts.CONTENT_STREQUENT_URI,它可以为您提供经常呼叫和已加星标的联系人。

+0

尝试使用ContentProvider .... – subrussn90 2012-01-20 18:31:15

相关问题