android步进计数器不工作,未出现在sensormanager中

4si2a6ki  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(481)

我试图在android中制作一个简单的步进计数器,我目前正在使用oreo并用Pixel2API29进行仿真。问题是它根本不起作用,当我调用sensormanager时,没有列出step\u计数器或step\u检测器的传感器。我按照不同的教程一步一步地,没有工作。有人知道问题出在哪里吗?这是我的代码和清单,还有我要说的清单,
我的活动stepsactivity.java

public class StepsActivity extends AppCompatActivity implements SensorEventListener {

    SensorManager sm;
    Sensor sensor;
    TextView tv_steps_count;
    int counter=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_steps);

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACTIVITY_RECOGNITION) == PackageManager.PERMISSION_DENIED){
            //ask for permission
            requestPermissions(new String[]{Manifest.permission.ACTIVITY_RECOGNITION},Sensor.TYPE_STEP_COUNTER);
        }

        tv_steps_count = (TextView) findViewById(R.id.tv_steps_count);

        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        if(sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)!=null){
            sensor = (Sensor) sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
        }else if(sm.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR)!=null){
            sensor =(Sensor) sm.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
        }

        findViewById(R.id.backBTN).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();

            }
        });
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
            tv_steps_count.setText(""+sensorEvent.values[0]);

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    @Override
    public void onPause() {
        stop();
        super.onPause();
    }

    @Override
    public void onResume() {
        start();
        super.onResume();
    }

    private void start() {
        sm.registerListener(this, sensor, sm.SENSOR_DELAY_FASTEST);
    }

    private void stop() {
        sm.unregisterListener(this);
    }
}

我的清单(只是其中与此有关的部分)

...
<uses-feature
        android:name="android.hardware.sensor.accelerometer"
        android:required="true" />
    <uses-feature
        android:name="android.hardware.sensor.stepcounter"
        android:required="true"/>
   <uses-feature
       android:name="android.hardware.sensor.stepdetector"
       android:required="true"/>

    <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />
    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <uses-feature
        android:name="android.hardware.sensor.accelerometer"
        android:required="true" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />
    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
...
<uses-feature
        android:name="android.hardware.sensor.accelerometer"
        android:required="true" />
    <uses-feature android:name="android.hardware.sensor.stepcounter"
        android:required="true"/>
    <uses-feature android:name="android.hardware.sensor.stepdetector"
        android:required="true"/>
...

还有一个屏幕截图,显示调试器上的问题

q3qa4bjr

q3qa4bjr1#

问题是仿真器不支持步进计数器或步进检测器。您需要在对这两个传感器具有硬件支持的真实设备上测试此功能。

相关问题