Android - 如何从通话记录中获取联系人?

发布于 2024-12-25 12:31:50 字数 2009 浏览 1 评论 0原文

我正在尝试从通话记录中获取联系人。我可以使用此代码从主要联​​系人获取联系号码:

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

它的作用是,当我们单击“联系人”按钮时,它会打开一个包含所有联系人的列表,用户可以选择任何联系人,所选联系人将添加到“至”场。我想做完全相同的事情,但我不想显示所有联系人,而是只想显示最近使用的联系人(通话记录)以供选择。

如果您也能告诉我们如何在小组中执行此操作,那就太好了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺ぐ爱和你が 2025-01-01 12:31:50

我用我自己的版本做到了这一点。我使用了一个对话框,并将光标移至通话记录。这是函数:

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

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();
}
岁吢 2025-01-01 12:31:50

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

You can use ContactsContract.Contacts.CONTENT_STREQUENT_URI which will give you both Frequently called and Starred contacts.

垂暮老矣 2025-01-01 12:31:50

从 API 21 开始可以使用这个: https://developer .android.com/reference/kotlin/android/provider/CallLog.Calls#CACHED_LOOKUP_URI

在 API 级别 21 静态值 CACHED_LOOKUP_URI 中添加了 CACHED_LOOKUP_URI:
String 用于查找与手机关联的联系人的缓存 URI
编号(如果存在)。

此值通常由拨号器应用程序填充以进行缓存
目的,因此不能保证它存在,并且可能不是最新的
如果与此号码关联的联系信息已更改。

From API 21 is possible to use this: https://developer.android.com/reference/kotlin/android/provider/CallLog.Calls#CACHED_LOOKUP_URI

CACHED_LOOKUP_URI added in API level 21 static val CACHED_LOOKUP_URI:
String The cached URI to look up the contact associated with the phone
number, if it exists.

This value is typically filled in by the dialer app for the caching
purpose, so it's not guaranteed to be present, and may not be current
if the contact information associated with this number has changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文