如何跨多个活动安全地实施 WakeLock

发布于 2024-10-03 07:07:58 字数 897 浏览 0 评论 0原文

我希望为我的应用程序的用户提供使用 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;
   }
}

我当前正在从我的 onPauseonResume 覆盖中调用它,因为我希望确保不会导致锁定当用户未主动使用我的应用程序时的手机。我的应用程序还有另外 3 个全屏视图。确保他们的 WakeLock 延续到我的应用程序的所有部分,同时仍然对手机的其余部分安全的最佳方法是什么?

我的第一个想法是在我的每个活动中重复相同的代码片段,尽管这看起来像是很多样板。我也无法使用 onStartonStop ,因为当我切换到另一个全屏 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来分手还会想你 2024-10-10 07:07:58

不要使用WakeLock——这更多地用于服务并且需要您持有额外的权限。

相反,请在 Activity 中的某些 View 上使用 setKeepScreenOn()。根据 SharedPreferenceIntent extra 在 onCreate() 中调用该方法,具体取决于您收集首选项的方式。

Don't use a WakeLock -- that's more for services and requires you to hold an extra permission.

Instead, use setKeepScreenOn() on some View in your activity. Call that in onCreate() based upon a SharedPreference or Intent extra, depending on how you are collecting the preference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文