Reputation: 791
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.
Upvotes: 0
Views: 232
Reputation: 910
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.
Upvotes: 0
Reputation: 15847
public class YourActivity extends Activity implements SensorEventListener
{
onCreate()...
public void onSensorChanged(SensorEvent event)
{
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
// your code here
}
}
}
Upvotes: 2
Reputation: 15052
You need to use a SensorEventListener. Here are some sample codes to get you started with.
Upvotes: 1