Skip to content

Instantly share code, notes, and snippets.

@MiraLak MiraLak/AccelerometerActivity.java Secret
Last active Aug 29, 2015

Embed
What would you like to do?
Android app: Basic accelerometer
public class AccelerometerActivity extends ActionBarActivity implements SensorEventListener{
private String restURL;
private TextView acceleration;
private CassandraRestApi cassandraRestApi;
//...
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
//get params passed from startActivity
Bundle extras = getIntent().getExtras();
if (extras != null) {
restURL = extras.getString(StartActivity.URL);
}
// define restApi endpoint and mapping
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(restURL)
.build();
cassandraRestApi = restAdapter.create(CassandraRestApi.class);
//...
}
//...
@Override
public void onSensorChanged(SensorEvent event) {
Acceleration capturedAcceleration = getAccelerationFromSensor(event);
//...
new SendAccelerationAsyncTask().execute(capturedAcceleration);
}
//...
/**
* Asyncronous task to post request to a Rest API.
*/
private class SendAccelerationAsyncTask extends AsyncTask<Acceleration, Void, Void>{
@Override
protected Void doInBackground(Acceleration... params) {
try {
cassandraRestApi.sendAccelerationValues(params[0]);
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.