0

I'm developing an app which requires the system to get the touch events even after the system goes to sleep mode or after the user locks the screen, I tried searching for a solution but it is hard to find one. Should I want to set any permissions or is there any inbuilt methods or can I override any methods to perform this functionality.

Bob
  • 881
  • 2
  • 10
  • 16
  • I'm not clear why we are using the native code to intercept the sleep event. Is it the only way or is there any other way to do it? – Bob Mar 26 '12 at 09:36

2 Answers2

3

I'm developing an app which requires the system to get the touch events even after the system goes to sleep mode or after the user locks the screen

Fortunately, this is not possible. Otherwise, the device would not be asleep, and battery life would suffer as a result.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I think the battery life is not much concerned in my app. I just want to make the app to respond for the touch event while Sleeping and when the screen is locked (using the power button). – Bob Mar 26 '12 at 10:55
  • @John: Fortunately, this is not possible. – CommonsWare Mar 26 '12 at 10:57
  • even by the way suggested by [JScoobyCed](http://stackoverflow.com/a/9869907/970272) – Bob Mar 26 '12 at 11:22
  • @John: If you actually take the time to read that answer, you will see that they are telling you how to not allow the device to fall asleep. This is **COMPLETELY OPPOSITE FROM WHAT YOU SAY THAT YOU WANT**. – CommonsWare Mar 26 '12 at 11:23
  • Yeah I got it, and can u please brief me why it is not possible? – Bob Mar 26 '12 at 11:36
  • @John: First, if the device is asleep, nothing runs at all -- the CPU is powered down. Second, you in the background cannot receive touch events, regardless of whether the device is running or not. – CommonsWare Mar 26 '12 at 11:53
  • so there is no other way to get the touch events when the device is asleep even-though we make the CPU running (PARTIAL_WAKE_LOCK). – Bob Mar 26 '12 at 12:02
  • @John: No. The only way you can get touch events if if the device is fully on and you are the foreground activity. – CommonsWare Mar 26 '12 at 12:09
  • 1
    CommonasWare is right. Mobile device OEMs also shutdown the actual touch screen hardware when the device goes to sleep (to save power). The system will not even physically measure the capacitive touch screen in this state. It is impossible to do this. – Andi Jay Jul 20 '12 at 17:17
  • @CommanasWare: I have one comment to CommansWare... please check this app: https://play.google.com/store/apps/details?id=wolf.uit.com.unlockbywave i guess this is the process running in forground and it also drain bettry. Am i Right? – Shreyash Mahajan Nov 11 '14 at 06:54
0

Here is a link that shows how to prevent the phone from sleeping. If you couple that with, say a black screen to 'pretend' the hone is sleeping but actually running your code. So your code can still intercept touch event

Then you need to install your app as a service and make it start when the device is turned on.

You will not need NDK or rooted device for that (sorry, got a short night :) )

Community
  • 1
  • 1
JScoobyCed
  • 10,203
  • 6
  • 34
  • 58
  • It will work when the system goes to sleep but I want the same to work even after the power button is pressed. – Bob Mar 26 '12 at 10:29
  • Agreed. You would need to code each cases. FOr the power button, just intercept the press of if by overriding 'dispatchKeyEvent(KeyEvent event)' and compare the event to 'KeyEvent.KEYCODE_POWER' – JScoobyCed Mar 26 '12 at 10:42