本文介绍了Android的 - 如何从通话记录中的联系人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从通话记录联系人。我可以用这个code得到主触头的联络号码:

I am trying to get contacts from call log. I can get the contact numbers from main contacts using this code :

    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;
}

当我们点击联系人按钮,它打开一个列表中的所有联系人,用户可以选择任何接触,并且选择的联系人将在收件人字段中添加这样做是什么。我想要做的完全一样的东西,但不是显示所有联系人,我想只有那些谁是最近使用过的(呼叫记录)进行选择,以显示。

What this does is when we click a "Contact" button it open a list with all the contacts, the user can select any contact and that selected contact will be added in the "To" field. I want to do the exactly same thing, but instead of displaying all the contacts i want to display only those who were recently used (call log) for selection.

另外这将是很好,如果你能告诉如何与组织也做到这一点。

Also it would be nice if you can tell how to do this with groups also.

推荐答案

我得到了这个打算用我自己的版本。我用一个对话框,递给光标到通话记录。下面是函数:

I got this going using my own version. i used a dialog and handed it the cursor to the call log. Here is the function:

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();
}

这篇关于Android的 - 如何从通话记录中的联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 16:55