是否有办法删除sharedPrefs在android重新安装应用程序时?



我发现这很奇怪,但是当我卸载我的应用程序并再次安装它时,它恢复了SharedPrefs,这是负责我的进度条进度的。我试过allow backup=false和android:fullBackupOnly=false,但它不起作用。

代码如下:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBinding = ActivityMainBinding.inflate(layoutInflater)
val view = mBinding.root
setContentView(view)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACTIVITY_RECOGNITION)
!= PackageManager.PERMISSION_GRANTED) {
showRationalDialogForPermissions()
} else {
val MY_PERMISSIONS_REQUEST_ACTIVITY_RECOGNITION = 1
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.ACTIVITY_RECOGNITION),
MY_PERMISSIONS_REQUEST_ACTIVITY_RECOGNITION)

}


loadData()
resetSteps()
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager

}



override fun onResume() {
super.onResume()
running = true

val stepSensor = sensorManager?.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)
if (stepSensor == null) {
Toast.makeText(
this,
"No sensor for step counter detected on this device",
Toast.LENGTH_SHORT
).show()
} else {
sensorManager?.registerListener(this, stepSensor, SensorManager.SENSOR_DELAY_UI)
}
}




override fun onSensorChanged(event: SensorEvent?) {
if (running) {
totalSteps = event!!.values[0]
val currentSteps = totalSteps.toInt() - previousTotalSteps.toInt()                                      // in resetSteps, previous total steps is equal to total steps, therefore 90 - 90 is 0
mBinding.tvStepsTaken.text = ("$currentSteps")
mBinding.progressCircular.apply {
setProgressWithAnimation(currentSteps.toFloat())
}
}
}

override fun onAccuracyChanged(p0: Sensor?, p1: Int) {

}

fun saveData() {
val sharedPrefs =
getSharedPreferences("myPrefs", Context.MODE_PRIVATE)
val editor = sharedPrefs.edit()
editor.putFloat("key1", previousTotalSteps)
editor.apply()
}

private fun loadData() {
val sharedPrefs =
getSharedPreferences("myPrefs", Context.MODE_PRIVATE)
val savedNum: Float = sharedPrefs.getFloat("key1", 0f)
Log.d("MainActivity", "$savedNum")
previousTotalSteps = savedNum

}

fun resetSteps() {
mBinding.tvStepsTaken.setOnClickListener {
Toast.makeText(this, "Long press to reset steps", Toast.LENGTH_SHORT).show()
}
mBinding.tvStepsTaken.setOnLongClickListener {
totalSteps = previousTotalSteps
mBinding.tvStepsTaken.text = 0.toString()
mBinding.progressCircular.progress = 0f


saveData()


true
}

}

所以当我再次安装应用程序时,进度条仍然计数preoustotalsteps,当我点击它时,它重启为0,但当再次打开应用程序时,它仍然在进度条中有preoustotalsteps。这就像在sharedPrefs中保存所有步骤,而不管调用resetSteps函数或重新安装应用程序本身。即使我在重新打开应用程序后手动删除缓存和数据,它也会恢复数据lol

您也可以在清单文件中指定android:fullBackupOnly=falseandroid:allowBackup=false

不,我不认为这个问题与SharedPreferences文件有关。这是传感器的性质。步数计数器传感器总是记录用户采取的步骤,无论你的应用程序是否存在。应用程序通过事件值访问这些数据,并将其分配给totalSteps变量。只有当手机重新启动时,这个值才会重置为零。当您重新安装应用程序时,您的共享偏好文件也会被删除,因此您的先前的oustotalsteps值也会被删除。但是传感器事件值不会重置,除非手机重新启动。

因此,在重新安装之后,代码加载步长计数器传感器事件值并将其赋值给totalSteps变量,并且先前的oustotalsteps为零。因此,totalSteps - previousTotalSteps=totalSteps这是显示在textView。

同样,从的代码中resetSteps()

mBinding.tvStepsTaken.setOnLongClickListener{
totalSteps = previousTotalSteps
mBinding.tvStepsTaken.text = 0.toString()
mBinding.progressCircular.progress = 0f

应该是previousTotalSteps = totalSteps,而不是其他方式。您实际上必须将totalSteps值分配给previousTotalSteps变量,以便totalSteps - previousTotalSteps在重置时变为零。由于您在重置的同时以其他方式赋值,totalSteps变量再次恢复到基于事件值的步长计数器。而不是你重置的样子。这就是为什么你totalSteps计数复位后甚至再现。

相关内容

  • 没有找到相关文章

最新更新