0

I'm trying to create an application that would stay awake as long as the user chooses it to - and it would even stay awake in the case of an orientation change.

Initially I did this by creating a wake lock when the user decides that the application should stay awake, and then releasing this lock at onPause(), so that it is not held on to forever. but, when there is a change in orientation, the activity in question is destroyed and a new one is created - and I don't see a way to 'hand over' the wake lock to the new instance.

of course one could save it in the saved instance state - but at the time the activity is destroyed, one doesn't know if it's for the reason of an orientation change, or if it destroyed for good.

is there a generic way to achieve this?

Ákos Maróy
  • 919
  • 2
  • 10
  • 19

5 Answers5

1

As mentioned also in Android: Wakelock & handling orientation change, you can use the FLAG_KEEP_SCREEN_ON flag in onCreate():

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

If you only need the wakelock for that one activity, that should be enough so you wouldn't even need the wakelock permission.

Community
  • 1
  • 1
koljaTM
  • 10,064
  • 2
  • 40
  • 42
0

You can call isFinishing() in onPause().

http://developer.android.com/reference/android/app/Activity.html#isFinishing%28%29

Simon
  • 14,407
  • 8
  • 46
  • 61
  • thanks for the tip. although it seems this is not that straightforward either. for example, a WakeLock is not Parcelable, so it's not straightforward to save in onSaveInstanceState(). moreover, the activity is still destroyed on an orientation change, even though it's not finishing :( – Ákos Maróy Feb 24 '13 at 08:02
0

I think you should be able to use:

http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

... to distinguish between a configuration change (orientation) and real termination

G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40
  • thanks - but I have an Activity, not a Fragment, and this seems to only work for a Fragments – Ákos Maróy Feb 24 '13 at 08:06
  • Right... Look here: http://developer.android.com/guide/components/fragments.html at the section on adding a Fragment wo/ a UI. The example does something very like what you are trying to do. – G. Blake Meike Feb 24 '13 at 15:22
0

I'm a bit late coming to this party, but here's how I did it...

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE, "DoNotDimScreen");

Followed by...

wl.acquire();

... when you need to actually activate the wake lock.

The 'PowerManager.ON_AFTER_RELEASE' flag pokes the user activity timer so the screen stays on for a little longer when this wake lock is released, which will be the case when your activity is Destroyed on a change of orientation.

Works perfectly for me!

DDSports
  • 400
  • 1
  • 14
0

Solution usable, if you need other than a SCREEN_BRIGHT_WAKE_LOCK: see my answer here.

Community
  • 1
  • 1
Oliv
  • 10,221
  • 3
  • 55
  • 76