How to add an item to Listview by shaking android device?

First, create a layout like below:

2016-02-10_14-35-27

Next, create an interface and name it “ShakeListener”

public interface ShakeListener {
public void onShake();
}

Next, we create a new class to handle event from sensor.
View “ShakeEventManager” class:

public class ShakeEventManager implements SensorEventListener{
private SensorManager sManager;
private Sensor s;
private int counter;
private long firstMovTime;
private ShakeListener listener; // interface.

private float gravity[] = new float[3];

private static final int MOV_COUNTS = 2;
private static final int MOV_THRESHOLD = 4;
private static final float ALPHA = 0.8F;
private static final int SHAKE_WINDOW_TIME_INTERVAL = 500;

public ShakeEventManager() {
}

public void setListener(ShakeListener listener) {
this.listener = listener;
}

@Override
public void onSensorChanged(SensorEvent event) {
float maxAcc = calcMaxAcceleration(event);
if (maxAcc >= MOV_THRESHOLD) {
if (counter == 0) {
counter++;
firstMovTime = System.currentTimeMillis();
} else {
long now = System.currentTimeMillis();
if ((now - firstMovTime) < SHAKE_WINDOW_TIME_INTERVAL)
counter++;
else {
resetAllData();
counter++;
return;
}
if (counter >= MOV_COUNTS){
if (listener != null)
listener.onShake();
}
}
}

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}

private float calcMaxAcceleration(SensorEvent event) {
gravity[0] = calcGravityForce(event.values[0], 0);
gravity[1] = calcGravityForce(event.values[1], 1);
gravity[2] = calcGravityForce(event.values[2], 2);

float accX = event.values[0] - gravity[0];
float accY = event.values[1] - gravity[1];
float accZ = event.values[2] - gravity[2];

float max1 = Math.max(accX, accY);
return Math.max(max1, accZ);
}

private float calcGravityForce(float currentVal, int index) {
return ALPHA * gravity[index] + (1 - ALPHA) * currentVal;
}

private void resetAllData() {
counter = 0;
firstMovTime = System.currentTimeMillis();
}

public void init(Context ctx) {
sManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE);
s = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
register();
}

public void register() {
sManager.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
}

public void deregister() {
sManager.unregisterListener(this);
}
}

Finally, return to your main activity:

public class MainActivity extends Activity implements ShakeListener{
private ShakeEventManager shakeEventManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
// init your list
// init your adapter
yourListView.setAdapter(yourAdapter);

shakeEventManager = new ShakeEventManager();
shakeEventManager.setListener(this);
shakeEventManager.init(this);
}

public void onShake() {
yourLIST.add(etNameInput.getText().toString());
yourADAPTER.notifyDataSetChanged();
}

@Override
protected void onResume() {
super.onResume();
shakeEventManager.register();
}

@Override
protected void onPause() {
super.onPause();
shakeEventManager.deregister();
}
}
Advertisements

One thought on “How to add an item to Listview by shaking android device?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s