2

我有一个应用程序,我想将传感器数据合并到其中。我的第一步是创建一个仅读取传感器数据并将其显示在屏幕上的应用程序。我所做的大部分研究都指向诸如Lars Vogel 的. 不幸的是,这就是我卡住的地方。我基本上已经将他的代码复制粘贴到我的项目中,虽然应用程序运行,但它不显示任何数据。

这是我正在使用的代码:

package com.example.sensortest;    

import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class sensortest extends Activity implements SensorEventListener{
    private SensorManager mSensorManager;
    private Sensor mSensor;
    RelativeLayout rlsensortest; 
    TextView tvOutput;
    Button btnStart, btnStop;
    Boolean blnScan=false;

    private Handler mHandler = new Handler();
    private long mStartTime=0L;
    private long lElapsedTime=0;

    private boolean color = false;
    private long lastUpdate;        

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        //mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);       

        LayoutInflater layoutInflater = this.getLayoutInflater();

        rlsensortest = (RelativeLayout) layoutInflater.inflate(R.layout.activity_asphalt_alert,null);       
        tvOutput = (TextView) rlsensortest.findViewById(R.id.txtOutput);        
        tvOutput.setText("Initialized...\n");

        btnStart = (Button) rlsensortest.findViewById(R.id.btnStart);
        btnStop = (Button) rlsensortest.findViewById(R.id.btnStop);

        btnStart.setOnClickListener(mButtonListener);
        btnStop.setOnClickListener(mButtonListener);

        lastUpdate = System.currentTimeMillis();

        setContentView(rlsensortest);        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_asphalt_alert, menu);
        return true;
    }
    // Create an anonymous implementation of OnClickListener
    private OnClickListener mButtonListener = new OnClickListener() {
        String strMessage ="";
        public void onClick(View v) {
            if (v == btnStart){
                strMessage = "Starting sensor scan.";
                blnScan=true;
            //  mStartTimer();

            } else if (v == btnStop){
                strMessage = "Stoping sensor scan.";
                blnScan=false;
            //  mStopTimer();
            }
            if (strMessage != "")
                Toast.makeText(rlsensortest.getContext(), strMessage, Toast.LENGTH_SHORT).show();

            strMessage = "";
        }
    };



    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
/*      String strEvent = "";
        strEvent = "this is a test";
        tvOutput.setText(tvOutput.getText() + strEvent + "Another line\n"); */

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            getAccelerometer(event);
        }       

    }
    private void getAccelerometer(SensorEvent event) {
        float[] values = event.values;
        // Movement
        float x = values[0];
        float y = values[1];
        float z = values[2];

        float accelationSquareRoot = (x * x + y * y + z * z)
            / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
        long actualTime = System.currentTimeMillis();
        if (accelationSquareRoot >= 2) //
        {
          if (actualTime - lastUpdate < 200) {
            return;
          }
          lastUpdate = actualTime;
          Toast.makeText(this, "Device was shuffed", Toast.LENGTH_SHORT)
              .show();
          if (color) {
            tvOutput.setBackgroundColor(Color.GREEN);

          } else {
            tvOutput.setBackgroundColor(Color.RED);
          }
          color = !color;
        }
      } 
}

此时,当手机摇晃时,它应该(至少)举杯表示事件发生了。知道我做错了什么吗?任何帮助表示赞赏

4

2 回答 2

2

看起来你从来没有对mSensorManager. 您需要先从中获取一个Sensor,然后才能收到SensorEventListener回调。获取/释放Sensors 通常发生在onResume/中onPause

Lars VogelSensor教程中的示例:

  @Override
  protected void onResume() {
    super.onResume();
    // register this class as a listener for the orientation and
    // accelerometer sensors
    mSensorManager.registerListener(this,
        mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
        SensorManager.SENSOR_DELAY_NORMAL);
  }

  @Override
  protected void onPause() {
    // unregister listener
    super.onPause();
    mSensorManager.unregisterListener(this);
  }
于 2012-08-31T12:21:39.310 回答
1

添加到您的活动:

@Override
protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this,
            mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
}
于 2012-08-31T12:20:20.880 回答