Android计步器 [英] Android step counter
本文介绍了Android计步器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试制作一个简单的Android应用程序来计算步骤.当我运行应用程序时,onResume方法中将调用else,这意味着它没有找到传感器.我正在2016年运行Api 22的Samsung J3上进行测试,我想知道代码中的问题还是手机没有传感器.如果是手机,是否有解决方法?
I am trying to make a simple Android application to count steps. When I run the application the else is called in the onResume method meaning it did not find the sensor. I am testing on a 2016 Samsung J3 running Api 22. I am wondering is the problem in my code or is it that the phone does not have the sensor. If it is the phone is there a workaround for it?
public class MainActivity extends AppCompatActivity implements SensorEventListener
{
private TextView counterTextView;
private SensorManager sensorManager;
private boolean isWalking;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counterTextView = findViewById(R.id.counterTextView);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
}
@Override
protected void onResume()
{
super.onResume();
isWalking = true;
Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if(countSensor != null)
{
sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
}
else
{
counterTextView.setText("WARNING SENSOR NOT FOUND");
}
}
@Override
protected void onPause()
{
super.onPause();
isWalking = false;
}
@Override
public void onSensorChanged(SensorEvent event)
{
if(isWalking)
{
counterTextView.setText(String.valueOf(event.values[0]));
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
}
推荐答案
对于与我处境相同的任何人,它都是我正在使用的设备.如果要使用所有设备,则可以使用加速度计.
For anyone in the same situation as me, it was the device I was using. If you want to work with all devices you can use the accelerometer.
这篇关于Android计步器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文