`Hello friends, good morning. What should I do to restart the pedometer? I saw many samples, but what they all had in common was to click on the desired view to restart the pedometer. But I do not want that to happen. The number of steps should be 0 every 24 hours. My codes: Thanks for the code to explain
class Home : Fragment(), SensorEventListener {
lateinit var binding: FragmentHomeBinding
private var sensorManager: SensorManager? = null
private var running = false
private var totalStep = 0f
private var previousTotalStep = 0f
//
private lateinit var mHandler: Handler
private var calories = 0.0
val todayDate: String = DateFormat.getDateInstance(DateFormat.MEDIUM).format(Date())
val dateForCalories = "$todayDate-kcal"
override fun onResume() {
super.onResume()
running = true
val stepSensor = sensorManager?.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)
sensorManager?.registerListener(this, stepSensor, SensorManager.SENSOR_DELAY_UI)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentHomeBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//load View model
detailsViewModel.messageTodayLiveData.observe(viewLifecycleOwner) {
binding.homeLayout.messageDay.text = it.message
}
materialDrawer()
loadData()
resetSteps()
calculateCalories()
sensorManager = requireContext().getSystemService(Context.SENSOR_SERVICE) as SensorManager
}
private fun resetSteps() {
}
private fun saveDate() {
Constant.editor(requireContext()).putFloat(STEPNUMBER, previousTotalStep).apply()
}
private fun loadData() {
previousTotalStep = Constant.getSharePref(requireContext()).getFloat(STEPNUMBER, 0f)
}
override fun onSensorChanged(event: SensorEvent?) {
if (running)
totalStep = event!!.values[0]
val currentSteps = totalStep.toInt() - previousTotalStep.toInt()
binding.homeLayout.txtSteps.text = ("$currentSteps")
binding.homeLayout.txtKilometers.text =
String.format(getString(R.string.meters_today), stepsToMeters(currentSteps))
binding.homeLayout.progressStep.apply {
setProgressWithAnimation(currentSteps.toFloat())
}
}
override fun onAccuracyChanged(p0: Sensor?, p1: Int) {
}
}
Service codes
class MyService : Service(), SensorEventListener {
private var sensorManager: SensorManager? = null
private var running = false
private var totalStep = 0f
private var previousTotalStep = 0f
val todayDate: String = DateFormat.getDateInstance(DateFormat.MEDIUM).format(Date())
private lateinit var sharedPref: SharedPreferences
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
try {
running = true
val stepSensor = sensorManager?.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)
sensorManager?.registerListener(this, stepSensor, SensorManager.SENSOR_DELAY_UI)
} catch (e: Exception) {
}
return START_STICKY
}
override fun onBind(p0: Intent?): IBinder? {
return null
}
override fun onSensorChanged(event: SensorEvent?) {
if (running) {
totalStep = event!!.values[0]
val currentSteps = totalStep.toInt() - previousTotalStep.toInt()
Constant.editor(this).putFloat(STEPNUMBER, previousTotalStep).apply()
}
}
override fun onAccuracyChanged(p0: Sensor?, p1: Int) {
TODO("Not yet implemented")
}
override fun stopService(name: Intent?): Boolean {
return super.stopService(name)
}
override fun onDestroy() {
val intent = Intent(this, MyPhoneReceiver::class.java)
sendBroadcast(intent)
super.onDestroy()
}
}
Broadcast codes
class MyPhoneReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action?.equals(Intent.ACTION_BOOT_COMPLETED, ignoreCase = true) == true) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(Intent(context, MyService::class.java))
} else {
context.startService(Intent(context, MyService::class.java))
}
}
}
}