传感器管理器不可用

时间:2017-05-04 21:12:27

标签: android

可以请任何人解释我哪里出错了?

我试图做一个计步器,但每次我得到"传感器管理器不可用"

这是我的代码     `package com.example.pfe.mymapapplication.ui.activity;

import android.app.Activity;
import android.content.Context; 
import android.hardware.*;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.pfe.mymapapplication.R;
import com.txusballesteros.widgets.FitChart;

public class LocationServiceActivity extends Activity implements SensorEventListener {

private SensorManager sensorManager;
boolean activityRunning;
static FitChart fitChart ;
private TextView count;
// app:showAsAction="ifRoom"
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location_service);
    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    count = (TextView) findViewById(R.id.count);
    fitChart = (FitChart)findViewById(R.id.fitChart);
    assert fitChart != null;
    fitChart.setMinValue(0f);
    fitChart.setMaxValue(250f);
    fitChart.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            return false;
        }
    });
}
@Override
protected void onResume() {
    super.onResume();
    activityRunning = true;
    Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    if (countSensor != null) {
        sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
    } else {
        Toast.makeText(this, "Sensor Manager is not available!", Toast.LENGTH_LONG).show();
    }
}

@Override
protected void onPause() {
    super.onPause();
    activityRunning = true;
    // if you unregister the last listener, the hardware will stop detecting step events
 //        sensorManager.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (activityRunning) {
        fitChart.setValue(event.values[0]);
        count.setText(""+ (int)event.values[0]+"M");
        Log.e("Distance" , ""+ event.values[0]);
    }
}

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

请帮帮我!!请注意我在导入android.annotation.SystemApi时出现此错误;它不会解决SystemApi

提前非常感谢你。

1 个答案:

答案 0 :(得分:0)

显然,您的设备没有步进计数器传感器。