I am new to Android, so I believe I made a mistake somewhere and is unable to fix it. I have a Main Activity, lets call it a Main menu. From this I start other activities by pressing the appropriate button to launch the activity I want. The problem is I use the onSensorchanged() in one of the launched activities. If the application is started the first time, the launched activity values behave as expected, but if I close the activity (in other words now I am back in the main menu) and a launch the activity again the values read from the onSensorchanged() jumps. Correct values are combined with wrong values. I use intents to launch the second activity and think that the problem is with this.
The main menu code:
public class MainMenuActivity extends Activity{
Button gps_btn,lineFollow_btn,man_control_btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
Debug.startMethodTracing("Navigation Trace");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_menu, menu);
return true;
}
@Override
protected void onPause()
{
super.onPause();
Debug.stopMethodTracing();
}
public void addListenerOnButton()
{
final Context context = this;
gps_btn = (Button) findViewById(R.id.GPS_btn);
gps_btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent intent = new Intent(context, AutoControl.class);
startActivity(intent);
}
});
lineFollow_btn = (Button) findViewById(R.id.line_follower_btn);
lineFollow_btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent intent2 = new Intent(context, CamDemoActivity.class);
startActivity(intent2);
}
});
man_control_btn = (Button) findViewById(R.id.man_ctrl_btn);
man_control_btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent intent2 = new Intent(context, ManualAct_PLC.class);
startActivity(intent2);
}
});
}
The code for the OnSensorChanged:
public void onSensorChanged(SensorEvent event)
{
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
gravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
geoMagnetic = event.values;
if (gravity != null && geoMagnetic != null)
{
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, gravity,geoMagnetic);
if (success)
{
/* Orientation has azimuth, pitch and roll */
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
float azimut = orientation[0];
updateSensorInfoView(azimut);
steerRobot(azimut);
System.out.println("Raw Sensor value:" + Double.toString(Math.toDegrees(azimut)));
System.out.println("Degree Sensor value:" + calculations.compassDegString(azimut));
}
}
}
Viewing the values logged, once the AutoControl Activity is launched again my sensor data is unexpected:
01-04 17:52:34.925: I/System.out(31387): Calculatebearing value:2.168326647225342
01-04 17:52:34.925: I/System.out(31387): Raw Sensor value:-116.11590698474976
01-04 17:52:34.925: I/System.out(31387): Degree Sensor value:244.0
01-04 17:52:34.955: I/System.out(31387): Calculatebearing value:173.96307878454928
01-04 17:52:34.955: I/System.out(31387): Raw Sensor value:72.0893408779263
01-04 17:52:34.955: I/System.out(31387): Degree Sensor value:72.0
01-04 17:52:35.065: I/System.out(31387): Calculatebearing value:108.5784565015826
01-04 17:52:35.065: I/System.out(31387): Raw Sensor value:137.473963160893
01-04 17:52:35.070: I/System.out(31387): Degree Sensor value:137.0
Notice the degree sensor value has jumped considerably in short time. I have an arrow that is drawn on the screen based on the calculated bearing angle, which jumps around like crazy once the activity is loaded the second time.
I am suspecting that a second instance is created of this activity, but used the android:launchMode="singleInstance" tag in the Manifest file to try and prevent this.
I have anyone else experienced such a problem? I dont seem to find similar problems on the net.