Search code examples
javaandroidsleepgesture-recognition

Detect Gesture with Phone sleeping


I am developing an SOS android app. I want to detect a gesture (for example several touches on screen) if the phone is in mode sleep/standby, and start a send of help request (for example send a sms). How can I detect this gesture? Someone that can help me? Thank you

---SOLUTION---- I found the solution here and this is my code:

1)in the main activity

getApplicationContext().startService(new Intent(this, UpdateService.class));

2)I create a service

public class UpdateService extends Service {

@Override
public void onCreate() {
    super.onCreate();
    // register receiver that handles screen on and screen off logic
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new PowerHookReceiver();
    registerReceiver(mReceiver, filter);
}

@Override
public void onStart(Intent intent, int startId) {
    boolean screenOn = intent.getBooleanExtra("screen_state", false);
    if (!screenOn) {
        // your code
    } else {
        // your code
    }
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}}

3) I create a PowerHookReceiver();

public class PowerHookReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {


    Intent i = new Intent(context, UpdateService.class);
    context.startService(i);
    //do what you want
}}

4) The manifest

<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />

 <receiver android:name=".PowerHookReceiver" >
    </receiver>

    <service android:name=".UpdateService" />

I hope it is usefull =)


Solution

  • If the screen is off (i.e. in standby), the hardware will not register any touch events so what you have asked for (detecting screen gestures) is not possible. When the device goes to standby/asleep, the touch screen is effectively shutdown to conserve battery power. The best you can do is detect an action that does wake the screen, e.g. one of the hardware buttons.

    Also, if you wanted to detect touch events for your app, the device would need to be on with your activity in the foreground. So again, screen gestures are a non-starter for an SOS type app.