gpt4 book ai didi

android - 从通话记录中获取最后一次通话的详细信息

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:41 24 4
gpt4 key购买 nike

我正在使用下面的代码从通话记录中获取最后一次通话的详细信息。

public static CallDetails getLastCallDetails(Context context) {

CallDetails callDetails = new CallDetails();

Uri contacts = CallLog.Calls.CONTENT_URI;
try {

Cursor managedCursor = context.getContentResolver().query(contacts, null, null, null, android.provider.CallLog.Calls.DATE + " DESC limit 1;");

int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int incomingtype = managedCursor.getColumnIndex(String.valueOf(CallLog.Calls.INCOMING_TYPE));


while (managedCursor.moveToNext()) {
String callType;
String phNumber = managedCursor.getString(number);
String callerName = getContactName(context, phNumber);
if(incomingtype == -1){
callType = "incoming";
}
else {
callType = "outgoing";
}
String callDate = managedCursor.getString(date);
String callDayTime = new Date(Long.valueOf(callDate)).toString();

String callDuration = managedCursor.getString(duration);

callDetails.setCallerName(callerName);
callDetails.setCallerNumber(phNumber);
callDetails.setCallDuration(callDuration);
callDetails.setCallType(callType);
callDetails.setCallTimeStamp(callDayTime);

}
managedCursor.close();

} catch (SecurityException e) {
Log.e("Security Exception", "User denied call log permission");

}

return callDetails;

}

问题是它返回最后一次调用而不是最后一次调用。我需要回最后一个电话。我用谷歌搜索,但无法找到完美的解决方案。请帮忙。提前致谢。

最佳答案

添加这一行 managedCursor.moveToFirst()

 public static CallDetails getLastCallDetails(Context context) {

CallDetails callDetails = new CallDetails();

Uri contacts = CallLog.Calls.CONTENT_URI;
try {

Cursor managedCursor = context.getContentResolver().query(contacts, null, null, null, android.provider.CallLog.Calls.DATE + " DESC limit 1;");

int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int incomingtype = managedCursor.getColumnIndex(String.valueOf(CallLog.Calls.INCOMING_TYPE));

if(managedCursor.moveToFirst()){ // added line

while (managedCursor.moveToNext()) {
String callType;
String phNumber = managedCursor.getString(number);
String callerName = getContactName(context, phNumber);
if(incomingtype == -1){
callType = "incoming";
}
else {
callType = "outgoing";
}
String callDate = managedCursor.getString(date);
String callDayTime = new Date(Long.valueOf(callDate)).toString();

String callDuration = managedCursor.getString(duration);

callDetails.setCallerName(callerName);
callDetails.setCallerNumber(phNumber);
callDetails.setCallDuration(callDuration);
callDetails.setCallType(callType);
callDetails.setCallTimeStamp(callDayTime);

}
}
managedCursor.close();

} catch (SecurityException e) {
Log.e("Security Exception", "User denied call log permission");

}

return callDetails;

}

关于android - 从通话记录中获取最后一次通话的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42436808/

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