3

This code delete history in today:

val cal = Calendar.getInstance()
cal.time = Date()
val endTime = cal.timeInMillis
cal.add(Calendar.DAY_OF_YEAR, -1)
val startTime = cal.timeInMillis    
val request = DataDeleteRequest.Builder()
                    .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
                   // .addDataType(DataType.TYPE_STEP_COUNT_DELTA)
                    .deleteAllData ()
                    .deleteAllSessions ()
                    .build()

Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
                    .deleteData(request)
                    .addOnSuccessListener {
                        Log.i(TAG, "Successfully deleted today's sessions") }
                    .addOnFailureListener {
                        // The deletion will fail if the requesting app tries to delete data
                        // that it did not insert.
                        Log.i(TAG, "Failed to delete today's sessions")
                    }

Result : logcat show message successfully:

03-02 18:12:54.949 15978-15978/vnit.com.testrealm I/StepCounter: Successfully deleted today's sessions

But i use function read data, it still exist:

private fun readData() {
        Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
                .readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
                .addOnSuccessListener { dataSet ->
                    val total = (if (dataSet.isEmpty)
                        0
                    else
                        dataSet.dataPoints[0].getValue(Field.FIELD_STEPS).asInt()).toLong()
                    Log.i(TAG, "Total steps: " + total)
                    txtStep.setText(total.toString())
                }
                .addOnFailureListener(
                        object : OnFailureListener {
                            override fun onFailure(e: Exception) {
                                Log.w(TAG, "There was a problem getting the step count.", e)
                            }
                        })
    }

I am trying to open Google fit app, it still display's old value. Why can't delete old history? Thanks all.

Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26
D T
  • 3,522
  • 7
  • 45
  • 89

0 Answers0