It fetched the last second call duration instead of last call duration and in some devices its working fine

             @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)
                }
        

I faced an issue with my code-
check this situation – suppose I call client 1 and the duration is 50 seconds and it fetched correctly now I call client 2 its duration is 2 minutes which also fetched successfully now when I again call client 1 he doesn’t pick up the call duration is fetched here wrong 50 seconds which is the last second call duration and not the last call which is 0.
it works in new devices that have the(top to bottom) call history but it creates issues on those devices have bottom-to-top call history

 **I also try this solution that returns the last-second call duration and not the last call but in some devices, it works fine where the devices have ToptoBottom call records.** -
                
                    @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
                      }
                *here query fetches call logs in descending order (CallLog.Calls.DATE + " DESC"), 
        according to the latest entries are at the top* 
                
                
                 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
                     }
                
                
                
                   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)
                    }
            
            
 **The query fetches call logs in descending order (CallLog.Calls.DATE + " DESC"), 
  according to the latest entries are at the top iterate through the call log entries to find the one with the latest timestamp.**

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật