I just want to finish current activity when i shake my device for 2 seconds. What should i do for that? Can anyone guide me please.
Asked
Active
Viewed 230 times
0
-
1I think by accelerometer api you would get threshold not time...... – Dheeresh Singh Jun 06 '12 at 09:03
3 Answers
2
public class YourActivity extends Activity implements SensorEventListener
{
onCreate()...
public void onSensorChanged(SensorEvent event)
{
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
// your code here
}
}
}

MAC
- 15,799
- 8
- 54
- 95
1
You need to use a SensorEventListener. Here are some sample codes to get you started with.

Community
- 1
- 1

Kazekage Gaara
- 14,972
- 14
- 61
- 108
0
On a while
loop, receive your Sensor data and decide whether you're in the middle of a shaking sequence. If so, you can keep a timestamp variable and pull the event timestamp in order to do this until 2 seconds have passed.
@gtumca-MAC showed you how to use the sensor events, just check the Google API documentation for further details, it's really well explained.

PeterGriffin
- 910
- 8
- 19