开发者

How do you safely implement WakeLock across multiple activites

开发者 https://www.devze.com 2023-01-26 19:08 出处:网络
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:

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 onS开发者_开发知识库top 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消