Android应用程序与屏幕锁定工作应用程序、屏幕、工作、Android

2023-09-05 04:24:04 作者:心事涙中流

例如我有一个应用程序,计算有多少时间拍照键为pressed。锁定的应用程序无法正常工作的屏幕后,那么有没有什么办法可以让计算presses当屏幕被锁定?

For example I have an app that counts how many time Camera button was pressed. After locking the screen the app is not working, so is there any way to keep counting presses when screen is locked?

推荐答案

运行的应用程序和服务都暂停,当手机被锁定,但你可以掌握WAKE_LOCK关键,做你的任务

Running Apps and services are pause when the phone is locked, but you can acquire WAKE_LOCK key and do your task

PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyWakeLock"); 
wakeLock.acquire();

这将持有的CPU开,即使屏幕关闭

this will hold the CPU open even if the screen off