Question

I have an Android application in which on one activity I am using Google Maps API V2 and I have a second activity which has an Accelerometer on it. I am switching between these two activities by using respective buttons. I am trying to use the Accelerometer to detect braking when the user is driving and place a marker on the Map when that happens.

If I start the Accelerometer activity and then switch to my Map activity I can see using Log.d that the Accelerometer keeps getting its values continuously from onSensorChanged so the activity is not being shut down which is good for me. For some reason though I can't seem to be able to grab the values on the Map activity as they're coming up as 0.0 0.0 0.0 or null null null depending on what I use. Can someone please tell me how I can do this?

UPDATE

Main part of Accelerometer code:

@Override
public void onSensorChanged(SensorEvent e) {
    // TODO Auto-generated method stub

    // alpha is calculated as t / (t + dT)
    // with t, the low-pass filter's time-constant
    // and dT, the event delivery rate      
    if (e.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        // - + +        
        gravity[0] = alpha * gravity[0] + (1 - alpha) * e.values[0];
        gravity[1] = alpha * gravity[1] + (1 - alpha) * e.values[1];
        gravity[2] = alpha * gravity[2] + (1 - alpha) * e.values[2];

        acceleration[0] = e.values[0] - gravity[0];
        acceleration[1] = e.values[1] - gravity[1];
        acceleration[2] = e.values[2] - gravity[2];{

        txtX.setText(""+Math.round((acceleration[0] * 100.0) / 100.0));
        txtY.setText(""+Math.round((acceleration[1] * 100.0) / 100.0));
        txtZ.setText(""+Math.round((acceleration[2] * 100.0) / 100.0));
}

I would like to get the Accelerometer values in the onLocationChanged listener of the Map activity so that I may place a marker as soon as deceleration is detected.

Was it helpful?

Solution

If you can see log messages telling you that you are still receiving data from sensor, you are probably still registered with the activity in the background. This is not a good idea, since you can drain your battery quickly (stop receiving data from sensor when you don't need it anymore).

To unregister, add this:

  @Override
  protected void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
  }

Back to your question, you need to register also with the Map activity in order to receive data from sensor. Another solution may be to receive data in Service.

You can find more information about sensors here: http://developer.android.com/guide/topics/sensors/sensors_overview.html

OTHER TIPS

In the end I had to create another class of the Accelerometer and turn it into a Background Service. The background service works great and am using a Broadcast from the Accelerometer service and a Broadcast receiver in the Map activity to get my data.

I got my Intents and putExtras and the broadcast manager in onSensorChanged of the Accelerometer service like so:

@Override
public void onSensorChanged(SensorEvent e) {
    // TODO Auto-generated method stub

    if (e.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        // - + +        
        gravity[0] = alpha * gravity[0] + (1 - alpha) * e.values[0];
        gravity[1] = alpha * gravity[1] + (1 - alpha) * e.values[1];
        gravity[2] = alpha * gravity[2] + (1 - alpha) * e.values[2];

        acceleration[0] = e.values[0] - gravity[0];
        acceleration[1] = e.values[1] - gravity[1];
        acceleration[2] = e.values[2] - gravity[2];

        //Log.d(TAG, ""+acceleration[0] + " "+acceleration[1] + " "+acceleration[2]);

        a = new Intent(ACCEL_UPDATE);
        a.putExtra(ACCEL_RESULT, acceleration[0]);
        a.putExtra(ACCEL_RESULT, acceleration[1]);
        a.putExtra(ACCEL_RESULT, acceleration[2]);
        manager = LocalBroadcastManager.getInstance(this);
        manager.sendBroadcast(a);
}

Note: ACCEL_RESULT and ACCEL_UPDATE are just strings by which the broadcast receiver can identify the values and intent of which they are coming from. And then I receive the broadcast in my Map activity in onCreate:

//Receiver of Accelerometer Service broadcasts
    broadcastReceiver2 = new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent){
            x = intent.getDoubleExtra(AccelService.ACCEL_RESULT, 0);
            y = intent.getDoubleExtra(AccelService.ACCEL_RESULT, 0);
            z = intent.getDoubleExtra(AccelService.ACCEL_RESULT, 0);

            x = round(x,3,BigDecimal.ROUND_HALF_UP);
            y = round(y,3,BigDecimal.ROUND_HALF_UP);
            z = round(z,3,BigDecimal.ROUND_HALF_UP);

            //Log.d(TAG, "" + x + " " + y + " " + z);
        }
    };

And using a Log.d I was able to verify that I am indeed receiving the values in the onLocationChanged listener as I wanted to :).

@petlack You were right with your first answer as you said I should use services so am giving you the tick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top