简体   繁体   中英

Is it possible to get Android accelerometer values inside onCreate method?

Is it possible to get Android accelerometer values inside onCreate method? I want it, so as to make some initializations after reading them. So far, I have registered a sensor listener to get those values when they change through the onSensorChanged callback. I have 3 float attributes ( lastx , lasty and lastz ) initialized to -1, so as to store the accelerometer values there upon change. In onCreate method, after registering my sensor listener, I am trying to check these values, but they are always equal to -1. Is there a way to get these values inside the onCreate method?

Here is my code:

public class MainActivity extends Activity implements SensorEventListener {
    private SensorManager sensorManager;

    TextView textView;
    StringBuilder builder = new StringBuilder();
    float lastx = -1;
    float lasty = -1;
    float lastz = -1;

    @Override
    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        textView = new TextView(this);

        SensorManager manager = (SensorManager) 
                     getSystemService(Context.SENSOR_SERVICE);

        Sensor accelerometer = manager.getDefaultSensor(
                                     Sensor.TYPE_ACCELEROMETER);

        if(!manager.registerListener(this, accelerometer,
                                     SensorManager.SENSOR_DELAY_GAME)){
        Log.d("ERROR: ", "NO accelerometer found!");
        }

        setContentView(textView);
        builder.setLength(0);

        builder.append(
           "X " + this.lastx + "\nY " + this.lasty + "\nZ " + this.lasty);
        textView.setText(builder.toString());

         Log.d(" * accelerometer values:",
            "X: " + this.lastx+", Y: " + this.lasty + ", Z: " + this.lastz);

    }

    public void onAccuracyChanged(Sensor sensor,int accuracy){

    }

    public void onSensorChanged(SensorEvent event){

            // check sensor type
            if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){

                 // assign directions
                 float x=event.values[0];
                 float y=event.values[1];
                 float z=event.values[2];

                 this.lastx = x;
                 this.lasty = y;
                 this.lastz = z;

                 // builder.setLength(0);
                 // builder.append("X " + x + "\nY " + y + "\nZ " + z);
                 // textView.setText(builder.toString());
      }
  }
}

I think the main user interface thread is used for both the onCreate and the onSensorChanged . So onSensorChanged can't run until onCreate has finished. Whatever initialisation you need to do, I suggest you arrange for it to be done on the first call to onSensorChanged , eg by doing the initialisation if the values are -1.

Initialize some variables first.

int value , count = 0 , newVal;

Inside onSensorChanged

if(count == 0){

            value = (int) event.values[0];

            count++;

        }

        newVal = (int) event.values[0] - value ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM