为什么sensorManager.registerListener无法成功注册计步器的监听器?

huangapple go评论70阅读模式
英文:

Why does sensorManager.registerListener fail to register a listener for Step Counter?

问题

在`MainActivity.java`文件中,您需要检查 `onResume` 方法中的传感器注册部分。当前的代码如下:

```java
Sensor countSensor = sensorManager
        .getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if (countSensor != null) {
    if (sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI)) {
        Toast.makeText(this, "registered", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this, "NOT registered", Toast.LENGTH_LONG).show();
    }
} else {
    Toast.makeText(this, "Count sensor not available", Toast.LENGTH_LONG).show();
}

但是,根据您提供的日志 E/SensorManager: registerListenerImpl sensorName:Step Counter,isWakeUpSensor:false,似乎存在问题。请确保您的设备支持计步器传感器,否则无法注册监听器。

同时,您的 AndroidManifest.xml 文件已包含以下行以声明使用计步器传感器:

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

这是正确的,但请确保您的设备实际支持计步器传感器。

如果您确定设备支持计步器传感器但仍然遇到问题,您可以尝试以下步骤:

  1. 确保您的应用具有正确的权限。
  2. 检查设备设置以查看是否启用了计步器传感器。
  3. 确保您的应用没有任何其他错误,可能会干扰传感器注册。

希望这些信息能帮助您解决问题。


<details>
<summary>英文:</summary>

I would like to figure out the cause of not registering a listener for a `step counter` sensor and how to overcome it.

**MainActivity.java**

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

private SensorManager sensorManager;
private TextView count;
boolean activityRunning;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    count = (TextView) findViewById(R.id.counter);

    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
}

@Override
protected void onResume() {
    super.onResume();
    activityRunning = true;
    Sensor countSensor = sensorManager
            .getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    if (countSensor != null) {
        if (sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI)) {
            Toast.makeText(this, &quot;registered&quot;, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, &quot;NOT registered&quot;, Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(this, &quot;Count sensor not available&quot;, Toast.LENGTH_LONG).show();
    }
    if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_SENSOR_STEP_DETECTOR)) {
        Toast.makeText(getApplicationContext(), &quot;STEP DETECTOR -&gt; SUPPORTED&quot;, Toast.LENGTH_LONG).show();
        Log.i(&quot;onResume&quot;, &quot;step detector is supported&quot;);
    } else {
        Toast toast = Toast.makeText(getApplicationContext(), &quot;STEP DETECTOR -&gt; NO&quot;, Toast.LENGTH_LONG).show();
        Log.i(&quot;onResume&quot;, &quot;step detector is NOT supported&quot;);
    }
}

@Override
protected void onPause() {
    super.onPause();
    activityRunning = false;
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (activityRunning) {
        count.setText(String.valueOf(event.values[0]));
        Toast.makeText(this, &quot;onSensorChanged&quot;, Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this, &quot;changed, else branch&quot;, Toast.LENGTH_LONG).show();
    }
}

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

}


**AndroidManifest.xml**

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-feature android:name="android.hardware.sensor.stepcounter" android:required="true"/>
<uses-feature android:name="android.hardware.SensorManager"/>
<uses-feature android:name="android.hardware.Sensor"/>
<uses-feature android:name="android.hardware.SensorEvent"/>
<uses-feature android:name="android.hardware.SensorEventListener"/>

&lt;application
    android:allowBackup=&quot;true&quot;
    android:icon=&quot;@mipmap/ic_launcher&quot;
    android:label=&quot;@string/app_name&quot;
    android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
    android:supportsRtl=&quot;true&quot;
    android:theme=&quot;@style/AppTheme&quot;&gt;
    &lt;activity android:name=&quot;.MainActivity&quot;&gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
&lt;/application&gt;

</manifest>


It shows a `Toast` with `NOT registered` and this error in `Logcat`

E/SensorManager: registerListenerImpl sensorName:Step Counter,isWakeUpSensor:false


P.S. It pops up the `STEP DETECTOR -&gt; SUPPORTED` toast for the check of the presence of this sensor. Is there something else that must be added to the manifest? Or probably, it is a wrong way of registering a listener?

</details>


# 答案1
**得分**: 1

尝试将活动识别权限添加到您的清单中:

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

如果您的应用目标为Android 10+(API 29或更高版本),您可能还需要在运行时请求权限:
https://developer.android.com/about/versions/10/privacy/changes#physical-activity-recognition

<details>
<summary>英文:</summary>

Try adding the activity recognition permission to your manifest:

    &lt;uses-permission android:name=&quot;android.permission.ACTIVITY_RECOGNITION&quot;/&gt;

If you app targets Android 10+ (API 29 or later), you may need to request the permission at runtime as well:
https://developer.android.com/about/versions/10/privacy/changes#physical-activity-recognition

</details>



huangapple
  • 本文由 发表于 2020年8月11日 16:01:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63353944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定