java步进计数传感器不能在实际设备上工作

92vpleto  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(421)

我有步进计数传感器的代码,但我在我的设备上运行此代码,它不工作

override fun init_events() {
sensoreManager=getSystemService(Context.SENSOR_SERVICE) as SensorManager
        if(sensoreManager!!.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)!=null)
        {

            sensor=sensoreManager!!.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)
            running=true
            showToastMessage("yes")
        }else{
           showToastMessage("no")
            running=false
        }
}
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
        Log.i("TEST_TEST","ssssssssssss")

    }
@SuppressLint("SetTextI18n")
    override fun onSensorChanged(event: SensorEvent?) {
        Log.i("TEST_TEST","ssssssssssss")
        if(event!!.sensor==sensor)
            showToastMessage("movment")

    }

    override fun onResume() {
        Log.i("TEST_TEST","f RESume")

        super.onResume()
        if(sensoreManager!!.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)!=null){
            sensoreManager!!.registerListener(this,sensor,SensorManager.SENSOR_DELAY_FASTEST)
            Log.i("TEST_TEST","not null")}
        else{
            Log.i("TEST_TEST","null")
        }
    }

    override fun onPause() {
        Log.i("TEST_TEST","f onPause")
        super.onPause()
        if(sensoreManager!!.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)!=null)
           sensoreManager!!.unregisterListener(this,sensor)
    }

这个代码有什么问题我的小米Note9Pro

qzwqbdag

qzwqbdag1#

在这段代码中,你必须接受android10的权限

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

相关问题