Skip to content

Instantly share code, notes, and snippets.

@iewnait
Created March 19, 2012 20:31
Show Gist options
  • Save iewnait/2126862 to your computer and use it in GitHub Desktop.
Save iewnait/2126862 to your computer and use it in GitHub Desktop.
Activity Class for AccelerometerDemo App on WIMM ONE
/*
* Copyright (C) 2012 WIMM Labs Incorporated
*/
package com.wimm.demo.accelerometerdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import com.wimm.framework.app.LauncherActivity;
/*
* This is a demo of accelerometer usage that will cycle the background color
* of a view each time a shake is detected.
*
*/
public class AccelerometerDemoActivity extends LauncherActivity implements SensorEventListener {
private static final String TAG = AccelerometerDemoActivity.class.getSimpleName();
// The array of background colors to cycle through.
private static final int[] mBackgroundColorArray = {
Color.BLUE, Color.GREEN, Color.RED, Color.YELLOW, Color.CYAN, Color.MAGENTA };
// An integer tracking the index of the current background color.
private int mBackgroundColorIndex = 0;
// The view whose background we will be changing.
private View mTargetView = null;
private SensorManager mSensorManager;
// We will track the last time we changed the background color and only
// update it again if a certain number of milliseconds has passed. This
// will make the color changes more deterministic: one shake = one change,
// rather than colors quickly cycling throughout shakes and stopping on
// something random.
private static final long MAX_COLOR_CHANGE_FREQUENCEY_MS = 500;
/*
* This threshold determine whether or not we would consider a force(measured in Gs)
* experienced by the device to be considered a shake.
*
*/
private static final long SHAKE_FORCE_THRESHOLD_IN_G = 2;
private long mLastUpdateMs = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Retrieve the view whose background color we want to change and give
// it an initial value.
mTargetView = findViewById(R.id.textView);
mTargetView.setBackgroundColor(mBackgroundColorArray[mBackgroundColorIndex]);
// Retrieve a SensorManager instance.
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
// The system does not disable the accelerometer automatically when the screen
// turns off. To help save power we will listen for screen change events and
// register/unregister for accelerometer updates accordingly.
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.SCREEN_DIM");
filter.addAction("android.intent.action.SCREEN_ON");
this.registerReceiver(mReceiver, filter);
}
//rest of code
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment