Detect Gesture with Phone sleeping [closed]

风格不统一 提交于 2019-12-29 06:37:27

问题


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 =)


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/21112375/detect-gesture-with-phone-sleeping

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