Disable keep screen on - android

I used:
getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
How do I resume to Default state (no-keep-on)?

I think this should do it:
getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
See API for details.

If you instead set a flag android:keepScreenOn="true" (documentation) only on the views that need to keep the screen on, you wouldn't need to reset the flag manually.

Another approach
getWindow().setFlags(this.getWindow().getFlags() & ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Also read this
and you can also set android:keepScreenOn="true" in the root View in xml.

Directly from documentation:
Note: You don't need to clear the FLAG_KEEP_SCREEN_ON flag unless you no longer want the screen to stay on in your running application (for example, if you want the screen to time out after a certain period of inactivity). The window manager takes care of ensuring that the right things happen when the app goes into the background or returns to the foreground. But if you want to explicitly clear the flag and thereby allow the screen to turn off again, use clearFlags(): getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON).

Related

I want to make my android application keep the screen on, but only at certain times. How do I do this?

I'm aware of 2 methods to make the screen stay on:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); in the .java file
android:keepScreenOn="true" in the layout xml tag
It seems that this keeps the screen on well, but I basically need to know how to reverse one of these so that I can toggle between keeping the screen on and setting it to the normal timeout period.
For the first thing, you could use clearFlags.
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
For the second thing, use setKeepScreenOn(false) in the layout.
You might want to consider using a WakeLock - see PowerManager.WakeLock
You can acquire different types by calling newWakeLock using PowerManager
You Can use the following single line to prevent screen timeout while your app is running
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

How to access the Window when not an activity?

I want to access the Window class so I can set the screen brightness on my phone. The problem is that the class I want to do this from is not an activity. Is it possible to do this without being an activity? I have a context and a content resolver, if that helps.
Thanks in advance!
You have two options here.
Start a new transparent Activity, adjust the brightness value (of both the Window and system settings), then call finish() on the Activity. This will steal focus from the user in some cases, no matter what flags you use.
Create a persistent transparent system-wide overlay using a Dialog and the flag WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY. This can cause odd issues like blocking the installation of apps.
In other words, there's no clean way of doing it.

Android: Is it possible to get notified when enter/quit TouchMode?

When system enters into TouchMode, I'd like to know which widget will lose focus. When system quits TouchMode, I'd also like to know which widget will get focus. Overriding onFocusChange() didn't satisfy me, since it couldn't tell TouchMode change, since it could happen in every mode, touch, trackball, key navigation, etc.
SDK said only one API View.isInTouchMode() there it is. So, is it possible to detect TouchMode change?
Long shot but you probably need to maintain states manually. So you keep a a flag , lets say isTouchMode which you can set every time any of the widgets are touched and unset when something gets focus.
Use ViewTreeObserver.addOnTouchModeChangeListener(). It will tell you when the mode changes.
http://developer.android.com/reference/android/view/ViewTreeObserver.html

Suspend orientation change

Documentation says: "a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy()."
I would like to suspend the orientation change, since it crashes my app if it was done in the middle of a a loop (of reading a file).
How can I do this? Also - looking for some kind of "onOrientationChnage" function :)
First, why not solve the initial problem instead?
90% of the times, the problem that you are facing (when changing orientation) is solved in this question:
Background task, progress dialog, orientation change - is there any 100% working solution?
Read it, correct your code and see if the problems are gone!
If you still want to do it anyway:
Quoting the documentation:
Note: Handling the configuration
change yourself can make it much more
difficult to use alternative
resources, because the system does not
automatically apply them for you. This
technique should be considered a last
resort and is not recommended for most
applications.
Still want to do it? Read the following link (where I also found the quotation):
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
If you rely on android system to use different layouts when you are on portrait/landscape mode, you'll need to start to handle these differences by yourself. If you don't use these features, you may get away without doing nothing fancy :)
If you always want a particular orientation, you can add android:screenOrientation="portrait" (or ="landscape" as an attribute to your tag in the manifest.
At run time, you can call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Temporarily non-rotatable Android Activities?

Is it possible to make an Activity temporarily not rotatable (like, turning it on/off in code, not in the manifest)?
One of my old apps crashes if you rotate while it's doing an HTTP lookup as the views are no longer attached when it returns.
One of these days I'll fix it proper, but in the mean time it'd be useful if I could just make the thing not-rotatable while it's doing the lookup.
I believe you can register to be notified of the orientation change events and override Activity.onConfigurationChanged. Register for orientation changes in the manifest with the configChanges attribute.
Then, of course, you can decide whether or not to rotate and call setRequestedOrientation.

Categories

Resources