Android 10无法注册Sensor.TYPE_STEP_COUNTER [英] Android 10 cannot register Sensor.TYPE_STEP_COUNTER

查看:111
本文介绍了Android 10无法注册Sensor.TYPE_STEP_COUNTER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个用于跟踪步骤的应用程序.

I am currently developing a Application to track Steps.

为了跟踪步骤,我正在使用手机的以下传感器: Sensor.TYPE_STEP_COUNTER

In order to track steps i am using the following Sensor of the phone: Sensor.TYPE_STEP_COUNTER

这对我测试过的所有设备都很好.最近,我有机会在Samsung S10设备上测试该应用程序,但是它不再跟踪步骤,而可以在Samsung S9的Android 9上运行.它也可以在运行Android 6的Google Nexus上正常运行.

This worked fine for all devices I tested it with. Recently I had the chance to test the application on Samsung S10 device, but it did not track the steps anymore whereas it worked on Android 9 of the Samsung S9. It also works fine on a Google Nexus running Android 6.

我在运行应用程序时发现以下警告:

  • 2020-01-06 17:13:30.381 24261-24261/?D/SensorManager:registerListener失败(1):: 17,SAMSUNG Step Counter Sensor,200000,0,

一些其他调试信息:

  • 传感器名称="SAMSUNG步进检测器传感器",供应商="Samsung Inc.",版本= 1,类型= 18,maxRange = 1.0,分辨率= 1.0,功率= 0.3,minDelay = 0

调试信息有些混杂,因为我测试了步数计数器传感器和步数检测器传感器都产生相同的结果.

我注册传感器的代码:

 private SensorEventListener sensorEventListener = new SensorEventListener() {
    /**
     * This Method gets called on each Sensor Trigger event
     *
     * @param sensorEvent Event created by Sensor
     */
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        //ME COUNTING STEPS
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {
        //Nothing to do here
    }
};

private SensorManager sensorManager;

/**
 * Function that initialises all the Sensors
 * Sets SensorManager
 * Sets Sensor to monitor to STEP Counter
 * Also Registers the Sensor
 */
private void initSensors() {
    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    assert sensorManager != null;
    Sensor stepSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);

    if (stepSensor == null) {
        createToastMessage("Sensor not found.");
        selectedFragment = new NoSensor_Fragment();
        activeFragment = 5;
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                selectedFragment).commit();
    } else {
        sensorManager.registerListener(sensorEventListener, stepSensor,
                SensorManager.SENSOR_DELAY_NORMAL);
        hasSensor = true;
    }
}

我找到了解决此问题的方法

就我而言,这是权限的简单错误.在Android 10上,您需要请求权限才能获得对步进传感器的访问权限.在阅读Android 10的更改时,我忽略了这一点.我在 Manifest.xml

In my case it was simple mistake with Permissions. On Android 10 you need to ask for Permissions to gain Access to the Step Sensors. Which is something I overlooked when reading the changes for Android 10. I Added the following to the Manifest.xml

    <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>

并在 onCreate 方法中添加了以下内容:

And added the following in the onCreate Method:

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

希望这可以帮助人们面对与我相同的问题.

Hope this helps people facing the same issue as me.

推荐答案

可能的解决方案

就我而言,这是权限的简单错误.在Android 10上,您需要请求权限才能获得对步进传感器的访问权限.在阅读Android 10的更改时,我忽略了这一点.我在 Manifest.xml

In my case it was simple mistake with Permissions. On Android 10 you need to ask for Permissions to gain Access to the Step Sensors. Which is something I overlooked when reading the changes for Android 10. I Added the following to the Manifest.xml

    <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>

并在 onCreate 方法中添加了以下内容:

And added the following in the onCreate Method:

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

希望这可以帮助人们面对与我相同的问题.

Hope this helps people facing the same issue as me.

这篇关于Android 10无法注册Sensor.TYPE_STEP_COUNTER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆