Skip to content

Instantly share code, notes, and snippets.

@MiraLak
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MiraLak/0242d2caa0df24bee28f to your computer and use it in GitHub Desktop.
Save MiraLak/0242d2caa0df24bee28f to your computer and use it in GitHub Desktop.
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