如何跨多个活动安全地实施 WakeLock
我希望为我的应用程序的用户提供使用 WakeLock
保持屏幕打开的能力。在我的主要活动中,我创建了以下函数:
protected void processWakeLock(int pauseResume) {
switch (pauseResume) {
case STATE_RESUME:
if (mKeepScreenOn) {
wakeLock.acquire();
}
break;
case STATE_PAUSE:
if (wakeLock.isHeld()) {
wakeLock.release();
}
break;
}
}
我当前正在从我的 onPause
和 onResume
覆盖中调用它,因为我希望确保不会导致锁定当用户未主动使用我的应用程序时的手机。我的应用程序还有另外 3 个全屏视图。确保他们的 WakeLock
延续到我的应用程序的所有部分,同时仍然对手机的其余部分安全的最佳方法是什么?
我的第一个想法是在我的每个活动中重复相同的代码片段,尽管这看起来像是很多样板。我也无法使用 onStart
和 onStop
,因为当我切换到另一个全屏 Activity 时,可见性会丢失。虽然也许最好
基于此处找到的图表和信息( http://developer .android.com/guide/topics/fundamentals.html )我没有看到更好的应用锁的方法。
I wish to provide the users of my application the ability to keep the screen on using a WakeLock
. In my main activity I have created the following function:
protected void processWakeLock(int pauseResume) {
switch (pauseResume) {
case STATE_RESUME:
if (mKeepScreenOn) {
wakeLock.acquire();
}
break;
case STATE_PAUSE:
if (wakeLock.isHeld()) {
wakeLock.release();
}
break;
}
}
I am currently calling it from my onPause
and onResume
overrides, as I wish to make certain I do not cause a lock on the user's phone when they are not actively using my application. My application has 3 other full screen views. What is the best way to ensure that their WakeLock
carries over to all portions of my application while still being safe to the rest of their phone.
My first thought is to duplicate the same code snippet in each of my activities though that seems like a lot of boiler plate. I can't use onStart
and onStop
either because visibility is lost when I switch to another full screen activity. Though perhaps it would be better to
Based on the diagram and information found here ( http://developer.android.com/guide/topics/fundamentals.html ) I don't see a better way to apply the lock.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用
WakeLock
——这更多地用于服务并且需要您持有额外的权限。相反,请在 Activity 中的某些
View
上使用setKeepScreenOn()
。根据SharedPreference
或Intent
extra 在onCreate()
中调用该方法,具体取决于您收集首选项的方式。Don't use a
WakeLock
-- that's more for services and requires you to hold an extra permission.Instead, use
setKeepScreenOn()
on someView
in your activity. Call that inonCreate()
based upon aSharedPreference
orIntent
extra, depending on how you are collecting the preference.