Java CallLog.Calls.CACHED_NAME对于某些保存的联系人总是返回null
我正在尝试在我的应用程序中显示呼叫日志详细信息,但是CallLog.Calls.CACHED_NAME对于某些联系人总是返回null,即使它是名为的已保存联系人。内置的通话记录正确显示了这些联系人的姓名 这是我的代码:Java CallLog.Calls.CACHED_NAME对于某些保存的联系人总是返回null,java,android,calllog,Java,Android,Calllog,我正在尝试在我的应用程序中显示呼叫日志详细信息,但是CallLog.Calls.CACHED_NAME对于某些联系人总是返回null,即使它是名为的已保存联系人。内置的通话记录正确显示了这些联系人的姓名 这是我的代码: protected customAdapRecent doInBackground(Void... params) { ContentResolver resolver = context.getContentResolver(); final
protected customAdapRecent doInBackground(Void... params) {
ContentResolver resolver = context.getContentResolver();
final String[] PROJECTION = new String[] {
// CallLog.Calls.CACHED_LOOKUP_URI,
CallLog.Calls.NUMBER,
CallLog.Calls.CACHED_NAME,
CallLog.Calls.TYPE,
CallLog.Calls.DATE,
CallLog.Calls.DURATION
};
Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, PROJECTION, null, null, CallLog.Calls.DATE + " DESC");
if(cursor.getCount() > 0)
{
int iNumber = cursor.getColumnIndex(CallLog.Calls.NUMBER);
int iName = cursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
int iType = cursor.getColumnIndex(CallLog.Calls.TYPE);
int iDate = cursor.getColumnIndex(CallLog.Calls.DATE);
int iDuration = cursor.getColumnIndex(CallLog.Calls.DURATION);
DateFormat datePattern = DateFormat.getDateInstance(DateFormat.FULL);
String number;
String name;
String type;
String date;
String duration;
String contactId;
String callIs = "None";
String prevDate = "";
int callType;
while (cursor.moveToNext())
{
if(cursor.getString(iName) == null)
Log.e("DEBUG: ", "Position: " + cursor.getPosition());
number = cursor.getString(iNumber);
name = cursor.getString(iName);
type = cursor.getString(iType);
String tempdate = cursor.getString(iDate);
Long tempDate = Long.parseLong(tempdate);
date = datePattern.format(tempDate);
if(prevDate.equalsIgnoreCase(date))
{
prevDate = date;
date = "";
}
else
prevDate = date;
//date = new Date(Long.valueOf(strdate));
duration = cursor.getString(iDuration);
callType = Integer.parseInt(type);
switch (callType)
{
case CallLog.Calls.OUTGOING_TYPE:
callIs = "OUT";
recentRow newRowO = new recentRow(number, name, date, duration, callIs);
listItem_recentOut.add(newRowO);
break;
case CallLog.Calls.INCOMING_TYPE:
callIs = "IN";
recentRow newRowI = new recentRow(number, name, date, duration, callIs);
listItem_recentIn.add(newRowI);
break;
case CallLog.Calls.MISSED_TYPE:
callIs = "MISS";
recentRow newRowM = new recentRow(number, name, date, duration, callIs);
listItem_recentMiss.add(newRowM);
break;
}
recentRow newRow = new recentRow(number, name, date, duration, callIs);
//recentRow newRow = new recentRow(number, name, callIs);
listItem_recentAll.add(newRow);
}
cursor.close();
cAdapRecent = new customAdapRecent(context, listItem_recentAll);
}
return cAdapRecent;
}
Log.e()中给出的调试语句也正在打印
我在查找中是否做错了什么?请。建议一个方法,因为我真的被阻止了
提前感谢…我也遇到了这种奇怪的行为,并发现有时你无法立即获得联系人的姓名,即使你可以从
CallLog.Calls
获取其他信息。我注意到的是,在大约一个小时左右的通话后,您可以获取姓名以及所有其他CallLog.Calls
数据。很奇怪。如果您需要在通话后立即刷新,您可以从联系人合同中获取电话号码的名称,如下所示:
fun getNameForNumber(context: Context, number: String): String? {
var res: String? = null
try {
val resolver = context.contentResolver
val uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number))
val c = resolver.query(uri, arrayOf(ContactsContract.PhoneLookup.DISPLAY_NAME), null, null, null)
if (c != null) {
if (c.moveToFirst()) {
res = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
}
c.close()
}
} catch (e: Exception) {
e.printStackTrace()
}
return res
}
奇怪的是,当你从联系人合同中取一个名字后,通过CallLog.Calls.CACHED_name以你的方式取名字,效果会很差
这也是@PeterB和@shyam002的答案,在打电话时,您的联系人中是否有这些人?CACHED_NAME
的目的是保存通话时已知的信息。是的,这些都是使用NAME保存的联系人。。安卓内置应用程序显示的是姓名。“这些是用姓名保存的联系人”——这不是我要问的。“以及显示姓名的Android内置应用”--它可能在contacts Contract
中查找值。是的,这些联系人在打电话时就在我的联系人中。我打电话时也遇到了同样的问题,在通话结束后,我得到了更新的通话日志列表,但我的姓名得到了空值,你得到答案了吗?