2012-01-08 76 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

相關問題