TYPE_STEP_COUNTER 在 NEXUS5X 29API 中找不到传感器
TYPE_STEP_COUNTER sensor not found in NEXUS5X 29API
我正在使用 Nexus5X API29 在 android 工作室中进行计步器项目。我需要为此使用传感器。我写了如下传感器代码,但它仍然给出 'Sensor not found' 错误。有什么办法可以解决这个问题吗?
public class MainActivity extends AppCompatActivity implements SensorEventListener {
TextView tv_steps;
SensorManager sensorManager;
Sensor sensor;
boolean running = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
tv_steps = (TextView) findViewById ( R.id.tv_steps );
sensorManager = (SensorManager) getSystemService ( Context.SENSOR_SERVICE);
}
@Override
protected void onResume() {
super.onResume ();
running = true;
Sensor countSensor = sensorManager.getDefaultSensor ( sensor.TYPE_STEP_COUNTER );
if(countSensor!= null){
sensorManager.registerListener ( this,countSensor,SensorManager.SENSOR_DELAY_UI );
}else {
Log.d ("Main Activity","SENSOR NOT FOUND" );
}
}
@Override
protected void onPause() {
super.onPause ();
running = false;
//if you unregister the hardware will stop detecting steps
}
@Override
public void onSensorChanged(SensorEvent event) {
if (running){
tv_steps.setText ( String.valueOf ( event.values[0] ) );
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
在清单文件中我也实现了这样的权限:
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" android:required="true"/>
我建议您在真实设备上测试此应用。通常,模拟器没有某些功能,例如传感器或文本转语音。
如果还是不行,请确保您的物理设备有步进传感器。
我正在使用 Nexus5X API29 在 android 工作室中进行计步器项目。我需要为此使用传感器。我写了如下传感器代码,但它仍然给出 'Sensor not found' 错误。有什么办法可以解决这个问题吗?
public class MainActivity extends AppCompatActivity implements SensorEventListener {
TextView tv_steps;
SensorManager sensorManager;
Sensor sensor;
boolean running = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
tv_steps = (TextView) findViewById ( R.id.tv_steps );
sensorManager = (SensorManager) getSystemService ( Context.SENSOR_SERVICE);
}
@Override
protected void onResume() {
super.onResume ();
running = true;
Sensor countSensor = sensorManager.getDefaultSensor ( sensor.TYPE_STEP_COUNTER );
if(countSensor!= null){
sensorManager.registerListener ( this,countSensor,SensorManager.SENSOR_DELAY_UI );
}else {
Log.d ("Main Activity","SENSOR NOT FOUND" );
}
}
@Override
protected void onPause() {
super.onPause ();
running = false;
//if you unregister the hardware will stop detecting steps
}
@Override
public void onSensorChanged(SensorEvent event) {
if (running){
tv_steps.setText ( String.valueOf ( event.values[0] ) );
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
在清单文件中我也实现了这样的权限:
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" android:required="true"/>
我建议您在真实设备上测试此应用。通常,模拟器没有某些功能,例如传感器或文本转语音。
如果还是不行,请确保您的物理设备有步进传感器。