我正在尝试存储用户的步数,但是当我重新启动或关闭移动设备时,数据就会消失。 SharedPreferences未保存数据。重新启动手机后进入应用程序时,它会变为0。怎么解决?
代码如下:
package com.threemusketeers.healthmaster;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Pedometer extends Activity implements SensorEventListener {
private TextView textView;
private SensorManager mSensorManager;
private Sensor mStepCounterSensor;
private Sensor mStepDetectorSensor;
SharedPreferences sharedPreferences;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pedometer);
textView = (TextView) findViewById(R.id.textview);
mSensorManager = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);
mStepCounterSensor = mSensorManager
.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
mStepDetectorSensor = mSensorManager
.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
}
public void onSensorChanged(SensorEvent event) {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
Sensor sensor = event.sensor;
float[] values = event.values;
int value = -1;
if (values.length > 0) {
value = (int) values[0];
}
if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
editor.putInt("steps", value).commit();
textView.setText("Step Counter Detected : " + sharedPreferences.getInt("steps", 0));
} else if (sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
// For test only. Only allowed value is 1.0 i.e. for step taken
editor.putInt("steps", value).commit();
textView.setText("Step Detector Detected : " + sharedPreferences.getInt("steps", 0));
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mStepCounterSensor,
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, mStepDetectorSensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
protected void onStop() {
super.onStop();
mSensorManager.unregisterListener(this, mStepCounterSensor);
mSensorManager.unregisterListener(this, mStepDetectorSensor);
}
}
答案 0 :(得分:0)
你做了一些奇怪的事情,即将值写入For Each Cell In NewData 'where NewData is the range with all of the headings
Range(Cell.Offset(1, 0), Cell.Offset(1, 0).End(xlDown)).Copy
Worksheets("Total").Activate
Worksheets("Total").Rows(2).Find(Cell.Value).End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
On Error Resume Next
'error handling skips data columns in sheet 1 that are not found in sheet 2
Next Cell
,然后再从那里得到它,这是一个不必要的IO操作。
由于步数计数器
,这也会导致您的问题上次重启后用户采取的步骤数
(来自documentation)
重启设备时,计数重置为0.您不更新SharedPreferences
中的TextView,而是更改步数更改(从0到1)覆盖SharedPreferences中的先前值从那里更新TextView的值。
如果当前步数低于保存值,则需要更改保存逻辑以首先从sharedprefs读取值并将其添加为基值。或者自己计算步进检测器的步数。
答案 1 :(得分:0)
尝试使用如下所述的SharedPreferences:
保存数据:
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("name", "Ravish");
editor.commit();
要检索数据:
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
String name= prefs.getString("name");