如何在android中检索最近的通话列表和收藏夹列表?
我需要通过单击相应的按钮来调用最近的通话列表和最喜爱的通话列表,并且需要在我自己的列表布局中使用数据。我是android新手,在这方面有很多问题。有人能帮我吗..提前感谢..获取最近的通话列表,您可以在android中使用CallLog。这是一个很好的教程。这也很有帮助 您可以将其用于以下所有传出呼叫:如何在android中检索最近的通话列表和收藏夹列表?,android,Android,我需要通过单击相应的按钮来调用最近的通话列表和最喜爱的通话列表,并且需要在我自己的列表布局中使用数据。我是android新手,在这方面有很多问题。有人能帮我吗..提前感谢..获取最近的通话列表,您可以在android中使用CallLog。这是一个很好的教程。这也很有帮助 您可以将其用于以下所有传出呼叫: Cursor cursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null, andro
Cursor cursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null, android.provider.CallLog.Calls.TYPE+"="+android.provider.CallLog.Calls.OUTGOING_TYPE, null,null);
对于所有类型的呼叫,请按如下方式使用:
Cursor cursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null, null, null,null);
使用一些额外的、有用的代码: getFavoriteContacts:
Map getFavoriteContacts(){
Map contactMap = new HashMap();
Uri queryUri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED};
String selection =ContactsContract.Contacts.STARRED + "='1'";
Cursor cursor = getContentResolver().query(queryUri, projection, selection,null,null);
while (cursor.moveToNext()) {
String contactID = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
intent.setData(uri);
String intentUriString = intent.toUri(0);
String title = (cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
contactMap.put(title,intentUriString);
}
cursor.close();
return contactMap;
}
getRecentContacts:
Map getRecentContacts(){
Map contactMap = new HashMap();
Uri queryUri = android.provider.CallLog.Calls.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
CallLog.Calls._ID,
CallLog.Calls.NUMBER,
CallLog.Calls.CACHED_NAME,
CallLog.Calls.DATE};
String sortOrder = String.format("%s limit 500 ", CallLog.Calls.DATE + " DESC");
Cursor cursor = getContentResolver().query(queryUri, projection, null,null,sortOrder);
while (cursor.moveToNext()) {
String phoneNumber = cursor.getString(cursor
.getColumnIndex(CallLog.Calls.NUMBER));
String title = (cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)));
if(phoneNumber==null||title==null)continue;
String uri = "tel:" + phoneNumber ;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
String intentUriString = intent.toUri(0);
contactMap.put(title,intentUriString);
}
cursor.close();
return contactMap;
}
您可以使用以下Java代码获取最近的呼叫列表:
Cursor phones = getActivity().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null,null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex(CallLog.Calls.NUMBER));
if (phoneNumber.length() > 6) {
String name = phones.getString(phones.getColumnIndex(CallLog.Calls.CACHED_NAME));
numbers.add(phoneNumber);
}
}
你好,Hiral,我使用相同的查询来获取所有类型的通话记录,但在使用双sim卡设备时,我遇到了一个小问题。我的代码不适用于双sim卡设备。你能帮我一下吗。