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.