Java Android service keep device awake to receive serial port commands

364 views Asked by At

I am creating a car computer with a tablet (Asus MemoPad ME572C). I have an arduino connected with the tablet through an usb to serial adapter. Now i need to be able to turn the screen on from standby.

What i tried is putting this in the oncreate of an activity

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON        
);

This is working but if i leave the tablet for a minute or so turned into standby, i can't wake it anymore. It looks like the tablet has stopped my service or just doesn't listen on the serial port.

I don't know how to debug this or solve it. Hope you can help.

PS: sorry my english isn't very good.

1

There are 1 answers

0
AudioBubble On

Note that for the method your using (WindowManager.LayoutParams flags) your activity needs to be fullscreen.

If that doesn't work try this answer. It's a depreciated method, so you don't want to keep it long-term.

Quoting from @Yar:

To wake up the screen:

PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK |

PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG"); wakeLock.acquire();

To release the screen lock:

KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE); 
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();

And the manifest needs to contain:

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

For more details about PowerManager, refer to the API documentation: http://developer.android.com/reference/android/os/PowerManager.html

If that doesn't work you'll need to investigate whether the service is being shut down. You should be able to prevent that happening by calling startForeground() on your service. See the documentation for explanation and sample code.