简体   繁体   English

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

[英]How do you safely implement WakeLock across multiple activites

I wish to provide the users of my application the ability to keep the screen on using a WakeLock . 我希望为我的应用程序的用户提供使用WakeLock保持屏幕打开的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. 我目前正在从onPauseonResume覆盖中调用它,因为我想确定当用户不积极使用我的应用程序时,不会导致用户手机的锁定。 My application has 3 other full screen views. 我的应用程序还有其他3个全屏视图。 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. 确保他们的WakeLock继承到我应用程序的所有部分,同时仍然对其余电话保持安全的最佳方法是什么。

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. 我也不能使用onStartonStop ,因为当我切换到另一个全屏活动时,可见性会丢失。 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. 根据此处的图表和信息( http://developer.android.com/guide/topics/fundamentals.html ),我看不到一种更好的方法来应用该锁。

Don't use a WakeLock -- that's more for services and requires you to hold an extra permission. 不要使用WakeLock这更多是用于服务,需要您额外的权限。

Instead, use setKeepScreenOn() on some View in your activity. 而是在活动中的某些View上使用setKeepScreenOn() Call that in onCreate() based upon a SharedPreference or Intent extra, depending on how you are collecting the preference. 根据SharedPreferenceIntent Extra在onCreate()调用,具体取决于您如何收集首选项。

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

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