I am pretty new to this but I was told I could get good help here. A friend and myself are playing around with creating Android apps (using ADT)
Here is how we are trying to make the program: in an activity, a user sets threshold values for the X and Y axis on the accelerometer. When the user hits button "Start", startService is invoked and starts TiltService.
TiltService is designed to run in the background always on the phone without user interaction. TiltService constantly compares the threshold with the accelerometer values and will vibrate if they are off.
My problem is I can't seem to get the putExtra() data correctly. I have overridden the onStartCommand in my service but I get the message "unreachable code" when I save the getExtras() to a bundle.
Here is the relevant code (I can post the whole thing, just do not want to clog up page)
TiltMeterActivity
private Intent dataIntent = null;
private float FORWARD_THRESHOLD = 4f;
private float BACKWARD_THRESHOLD = -3f;
private float UPWARD_THRESHOLD = 3f;
private float DOWNWARD_THRESHOLD = -3f;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bstart = (Button)findViewById(R.id.btnStart);
dataIntent = new Intent();
dataIntent.setClass(TiltMeterActivity.this, TiltService.class);
dataIntent.putExtra("fowardThreshold", FORWARD_THRESHOLD);
dataIntent.putExtra("backwardThreshold", FORWARD_THRESHOLD);
dataIntent.putExtra("upwardThreshold", UPWARD_THRESHOLD);
dataIntent.putExtra("downwardThreshold", DOWNWARD_THRESHOLD);
bStart.setOnclickListener(new View.OnClickListener() {
public void onClick(View v) {
startService(dataIntent);
}
});
}
TiltService
private Bundle thresholdData = new Bundle();
private float Z_FORWARD_THRESHOLD = 0f;
private float Z_BACKWARD_THRESHOLD = 0f;
private float Y_UPWARD_THRESHOLD = 0f;
private float Y_DOWNWARD_THRESHOLD = 0f;
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
thresholdData = intent.getExtras(); // The problem area (Eclipse says this is unreachable code)
Z_FORWARD_THRESHOLD = thresholdData.getFloat("forwardThreshold");
Z_BACKWARD_THRESHOLD = thresholdData.getFloat("backwardThreshold");
Y_UPWARD_THRESHOLD = thresholdData.getFloat("upwardThreshold");
Y_DOWNWARD_THRESHOLD = thresholdData.getFloat("downwardThreshold");
return START_STICKY;
}
I thought I understood the basic of how Intents could pass data, but I guess I don't. Is it obvious what I am missing? Thanks for any help
put the
return
statement in youronstartcommand
to the last line of the function.I think you should use
thresholdData.getString(forwardThreshold)
to get the data.