gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:09:30 32 4
gpt4 key购买 nike

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

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

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

另外,如果您也能告诉我们如何对群组执行此操作,那就太好了。

最佳答案

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

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

关于java - Android - 如何从通话记录中获取联系人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8775838/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com