Make Android Screen never dim while on a particular activity? - android

Going through logs from 2009, it seems that everyone was complaining that this wasn't an option.
Now all I'm finding is people telling people different ways to do it, none of which work on my device. keepScreenOn, or its corresponding flag in LayoutParams, only serves to keep the screen on, even though multiple people have offered it as a solution for screen dimming.
WakeLocks, on the other hand (as I have seen them demonstrated online, anyway), appear to do absolutely nothing, not even keep the screen alive. The screen dims at the normal time, and I even get to see this hilarious little number on the monitor I have running on my laptop:
02-04 00:10:30.687: D/PowerManagerService(192): #PowerManagement: 'MyActivitiyName' releaseWakeLock when screen locked
I made sure to follow all the wakelock instructions on this page: http://blog.blundell-apps.com/tut-keep-screen-onawake-3-possible-ways/
There were no build errors, and no runtime errors, either. The screen just dims and then shuts off, in the standard time frame.
Help?

Try by adding following statement in onCreate() method. Hope this will work.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Okay, I was being dumb about the flags, following tutorials too blindly. I assumed the example wakelock flag was supposed to make the screen not dim, but it was the opposite. I switched the wakelock flag to
PowerManager.SCREEN_BRIGHT_WAKE_LOCK
and moved the wakelock acquire and release to onCreate and onDestroy, and now it does what I want.
(to be clear, this does what I want because the current activity is the final state I need for my demo; onDestroy should be called whenever I would possibly want to pause the activity)

Related

In Unity3D how turning off screen whithout sleep?

As in Unity3D turn off the screen without entering sleep mode, so that the application continues to work?
I don't believe it's possible to have Unity run in the background.
I don't know if this is still common practice, but in the past, many people simply draw a fully black screen to conserve power.
See: https://forum.unity.com/threads/application-runinbackground-is-not-working-on-android.117723/
Have you looked at - https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationFocus.html
OnApplicationFocus is called when the application loses or gains focus. Alt-tabbing or Cmd-tabbing can take focus away from the Unity application to another desktop application. This causes the GameObjects to receive an OnApplicationFocus call with the argument set to false. When the user switches back to the Unity application, the GameObjects receive an OnApplicationFocus call with the argument set to true.
You could implement this with the gameobjects you want to continue behaving as you like.

controlling screen lock and display brightness from a Service (no Activity)

I've seen two other similar questions, but no answers that work.
Control screen brightness in android using " Background Service"
Android Development: Changing Screen Brightness in Service
I have a background service that is trying to do some custom power management, and needs to be able to independently control the screen locking (either forcing it to lock immediately or keep it from locking) and also the display state (force it to turn off, on, or dim). I'm having a hard time finding such controls. We're developing for a device, so one option is to modify Android source code, but we're trying very hard to avoid that.
I can use PowerManager.WakeLocks to passively suggest screen brightness (and so can anyone else, so it's not guaranteed). This is okay for preventing screen lock and keeping the display on.
I did see a PowerManager.goToSleep() call for forcing the lock screen - that works. But it doesn't force the display to turn off (or something else is preventing that).
Are there any Android OS API's for this? I can't find them in the online dev guide.
To modify the brightness, I'm using
import android.provider.Settings.system;
...
ContentResolver contentResolver = Application.getContext().getContentResolver();
...
System.putInt(contentResolver, System.SCREEN_BRIGHTNESS, value);

screen goes off while recording and preview gets corrupted

How do you prevent the screen from going off when recording is in process. If recording is in process, and there's no other activity like using arrow keys, or the keypad, the screen goes off and when it's brought back to life, the preview is corrupted.
What do you mean with "recording in progress"? You have an app that is recording something?
The easiest way (which doesn't require any additional permissions) is to give any of your views android:keepScreenOn=true (there's also setKeepScreenOn()).
If you want to be hardcore, you can request a wakelock permission, but that should not be necessary - if you have a UI, keepScreenOn is a much more elegant solution.

What's the proper way to handle the Activity lifecycle with OpenGL

I'm really struggling here, and it's holding me back.
What is the correct way to handle OpenGL, and an Activity - which launches sub-activities, or goes back to the home screen. And have it resume just exactly where it was.
I have it semi-working as it stands, textures/VBOs are reloaded at onResume() when needed.
But sometimes, when launching sub-activities and returning, or going to home screen and returning, onCreate is being triggered again. This messes up the whole thing, and I end up with a black screen.
I'm sure I'm doing the whole thing wrong. Can someone explain how one should handle an Activity like this?
What platform are you working with?
The reason I ask is that prior to Eclair this whole area was riddled with bugs with the result that suspending/resuming OpenGL basically only worked by accident. However, these do seem to have been fixed by Eclair and our app seems to be suspending and resuming fairly reliably.
What you're supposed to do is to register a callback to your SurfaceHolder so you get notified when the surface appears and disappears. In the surfaceDestroyed() method you shut down EGL completely, and then in your surfaceCreated() method you reinitialise it. You shouldn't be doing any of this from your Activity's onCreate()/onResume() methods, as the surface may not appear and disappear at the same time.
That said, our application was designed for Cupcake, when things were pretty primitive. I gather that these days there are utility classes available that do all the heavy lifting for you, so if you're using one of those things may work differently; and if you're not, you may want to look into them.

Black screen is displaying in Android

I opened my application after I am not doing any kind of operation. I leave the mobile 5 min. After 5 min when I touch the my application it is showing black screen. Is there any way to avoid this idle state?
This is easy to do. Use: getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); in your onCreate method.
There is another way, called a WakeLock, but you really shouldn't use that unless you know what you're doing. See this post from one of the Android designers for more information about WakeLocks and why you shouldn't use them unless you're careful.

Categories

Resources