0

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.

coderslay
  • 13,960
  • 31
  • 73
  • 121
SANDHYA
  • 771
  • 7
  • 18
  • 27

3 Answers3

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