1
可以請任何人解釋我哪裏出錯了?傳感器管理器不可用
我試圖做一個計步器,但每次我得到的「傳感器管理器不可用」
這是我的代碼 `包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
非常感謝你提前。
沒有其實我下載了一個步驟計數器的應用程序,它工作得很好,再次我說我可能有一個註解問題。SystemApi,因爲它不解決SystemApi –
@NefissaKhedher:「我下載了一個步計數器應用程序它工作得很好「 - 這不會幫助你」傳感器管理器不可用「的問題,這是因爲你的設備沒有步驟計數器傳感器,如果你的問題中的代碼是準確的。 「因爲它不解析SystemApi」 - 你的代碼不會引用那個符號。也許你的問題沒有導致你的問題的代碼。 – CommonsWare