SANDHYA
SANDHYA

Reputation: 791

How to finish an activity on device shaken in android

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

Answers (3)

PeterGriffin
PeterGriffin

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

MAC
MAC

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

Kazekage Gaara
Kazekage Gaara

Reputation: 15052

You need to use a SensorEventListener. Here are some sample codes to get you started with.

Upvotes: 1

Related Questions