Код: Выделить всё
@SuppressLint("Range")
private suspend fun lastCallDetail(): String {
val delayDuration = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) 3000L else 2000L
delay(delayDuration)
var callDuration = "0"
try {
val cur: Cursor? = contentResolver.query(
CallLog.Calls.CONTENT_URI,
null, null, null, CallLog.Calls.DATE + " DESC limit 1;"
)
if (cur != null) {
while (cur.moveToNext()) {
callDuration = cur.getString(cur.getColumnIndex(CallLog.Calls.DURATION))
break
}
cur.close()
}
} catch (e : Exception) {
e.printStackTrace()
}
return callDuration
}
private suspend fun lastCallIncoming(): String {
delay(3000)
var callDuration = "00:00:00"
try {
val cur = applicationContext.contentResolver.query(
CallLog.Calls.CONTENT_URI,
null,
null,
null,
CallLog.Calls.DATE + " DESC"
)
val duration = cur!!.getColumnIndex(CallLog.Calls.DURATION)
while (cur.moveToNext()) {
callDuration = cur.getString(duration)
Log.d("callTracking", callDuration)
break
}
cur.close()
val timeSec = callDuration.toInt() // Json output
callDuration = timeSec.toString() //hh:mm:ss formatted string
Log.d("callTracking", callDuration)
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return callDuration
}
private fun secToTime(second: Int): String {
var sec = second
val hours = sec / 3600
val minutes = sec % 3600/ 60
sec %= 60
return String.format("%02d:%02d:%02d", hours, minutes, sec)
}
Код: Выделить всё
@SuppressLint("Range")
private suspend fun lastCallDetail(): String {
val delayDuration = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) 3000L else 2000L
delay(delayDuration)
var callDuration = "0"
try {
val callLogCursor: Cursor? = contentResolver.query(
CallLog.Calls.CONTENT_URI,
null, null, null, CallLog.Calls.DATE + " DESC limit 1;"
)
if (callLogCursor != null) {
// Check the order of the call logs
val isOrderTopToBottom = determineCallLogOrder(callLogCursor)
// Move accoreding to the determined order
callLogCursor.moveToFirst()
val durationIndex = callLogCursor.getColumnIndex(CallLog.Calls.DURATION)
if (durationIndex != -1) {
if (isOrderTopToBottom) {
callDuration = callLogCursor.getString(durationIndex)
} else {
callLogCursor.moveToLast()
callDuration = callLogCursor.getString(durationIndex)
}
}
callLogCursor.close()
}
} catch (e: Exception) {
e.printStackTrace()
}
return callDuration
}
@SuppressLint("Range")
private suspend fun lastCallIncoming(): String {
delay(3000)
var callDuration = "00:00:00"
try {
val callLogCursor = applicationContext.contentResolver.query(
CallLog.Calls.CONTENT_URI,
null,
null,
null,
CallLog.Calls.DATE + " DESC"
)
if (callLogCursor != null) {
// Check the order of the call logs
val isOrderTopToBottom = determineCallLogOrder(callLogCursor)
callLogCursor.moveToFirst()
val durationIndex = callLogCursor.getColumnIndex(CallLog.Calls.DURATION)
// Move according to the determined order
if (durationIndex != -1) {
if (isOrderTopToBottom) {
callDuration = callLogCursor.getString(durationIndex)
} else {
callLogCursor.moveToLast()
callDuration = callLogCursor.getString(durationIndex)
}
val timeSec = callDuration.toInt()
callDuration = secToTime(timeSec)
Log.d("callTracking", callDuration)
}
callLogCursor.close()
}
} catch (e: Exception) {
e.printStackTrace()
}
return callDuration
}
private fun determineCallLogOrder(cursor: Cursor): Boolean {
var isOrderTopToBottom = true
if (cursor.moveToFirst()) {
val dateColumnIndex = cursor.getColumnIndex(CallLog.Calls.DATE)
if (dateColumnIndex != -1) {
val firstTimestamp = cursor.getLong(dateColumnIndex)
if (cursor.moveToNext()) {
val secondTimestamp = cursor.getLong(dateColumnIndex)
isOrderTopToBottom = firstTimestamp > secondTimestamp
}
}
}
return isOrderTopToBottom
}
это работает на новых устройствах, которые имеют (вверх внизу) история вызовов, но это создает проблемы на этих устройствах, история вызовов идет снизу вверх.
Запрос извлекает журналы вызовов в порядке убывания (CallLog.Calls.DATE + " DESC"),
в соответствии с последними записями вверху перебирает записи журнала вызовов, чтобы найти запись с самой последней отметкой времени
но, тем не менее, на некоторых устройствах он извлекает предпоследнюю продолжительность вызова, а на некоторых устройствах он работает хорошо, пожалуйста, помогите.
Подробнее здесь: https://stackoverflow.com/questions/786 ... -and-in-so