Detect Gesture with Phone sleeping [closed]

末鹿安然 提交于 2019-11-29 05:22:37

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.

Note : Phone will not detect any touch event on screen when it is in standby mode or sleeping mode. So the only option is to get events from any hardware button via register intent action in Broadcast Receiver.

You can generate SOS by hooking power button. Register a Broadcast Receiver which is initiated when power button is clicked.

Now in onReceive() method of the Receiver do what you want. Below Code For Broadcast receiver Class :

public class PowerHookReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

         Log.v("#@%@%#", "Power button is pressed.");  

         Toast.makeText(arg0, "power button clicked",Toast.LENGTH_LONG).show();

        //perform what you want here
    }
}

Register this receiver in Manifest :

 <receiver android:name="com.PowerHookReceiver">
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_OFF"></action>
            <action android:name="android.intent.action.SCREEN_ON"></action>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
             <action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
        </intent-filter>
    </receiver>

Now perform your task in onReceive() method of the Receiver.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!