简体   繁体   English

Java Android 服务保持设备唤醒以接收串口命令

[英]Java Android service keep device awake to receive serial port commands

I am creating a car computer with a tablet (Asus MemoPad ME572C).我正在用平板电脑(华硕 MemoPad ME572C)创建车载电脑。 I have an arduino connected with the tablet through an usb to serial adapter.我有一个通过 USB 转串口适配器与平板电脑连接的 arduino。 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我试过的是把它放在一个活动的oncreate中

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. PS:对不起,我的英语不是很好。

Note that for the method your using (WindowManager.LayoutParams flags) your activity needs to be fullscreen.请注意,对于您使用的方法(WindowManager.LayoutParams 标志),您的活动需要全屏显示。

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:引自@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.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG"); PowerManager.ACQUIRE_CAUSES_WAKEUP), "标记"); 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有关 PowerManager 的更多详细信息,请参阅 API 文档: 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.您应该能够通过在您的服务上调用startForeground()来防止这种情况发生。 See the documentation for explanation and sample code.有关说明和示例代码,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM