Sensing a Disturbance
We do not want to be tying up the accelerometer when LunchList does not
have the foreground. One way to do this is to override onResume() and
initialize our Shaker there:
@Override
public void onResume() {
super.onResume();
shaker=new Shaker(this, 1 .45d, 500 , onShake);
}
...then release our Shaker in an implementation of onPause():
@Override
public void onPause() {
super.onPause();
shaker.close();
}
Now when you shake the device, it will invoke our do-nothing callback.
Step #3: Make a Random Selection on a Shake................................
To actually choose a restaurant, we need to ask our RestaurantAdapter how
many restaurants there are, then use Math.random() to pick one. We can
then package that in an Intent and start our DetailForm on that restaurant.
In particular, we ask our adapter for the _id value (getItemId()) of the
randomly-chosen restaurant, which gets used as the primary key when
DetailForm looks for it in the database.
With that in mind, here is a revised implementation of the onShake callback
object:
private Shaker.Callback onShake=new Shaker.Callback() {
public void shakingStarted() {
// no-op – only care when the shaking stops
}
public void shakingStopped() {
int selection=(int)(adapter.getCount()*Math.random());
Intent i=new Intent(LunchList.this, DetailForm.class);