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

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);

Related

Is it possible to write a script that allows you to change the brightness from the lock-screen?

I am looking to write a basic Android script, for when you hold 2-3 seconds on your device's lock-screen, it will set the brightness to 100. Is this possible on most modern Android devices, and what are some tips I can use to achieve this?
I am specifically looking for these features:
The ability to set the system's brightness to "x" on the lock-screen
The ability to detect if the user has held their finger in a specific spot for "y" amount of time on the lock-screen.
setting brightness with Settings.System.SCREEN_BRIGHTNESS is possible when you acquire proper permission (hints: ACTION_MANAGE_WRITE_SETTINGS, Settings.System.canWrite(context)). BUT on lock screen you can show ONLY Notification, which can't have a seekbar/progressbar controlled by user manually (it can only show progress and on newest OS versions can seek playback if your app is handling audio focus and MediaSession). Instead you can introduce some action buttons, e.g. three options: lowest brightness, auto, max brightness
this is not possible on lock screen. you could achieve this in whole not-locked system (any app) with AccessibilityService, but it will run only when device is unlocked

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.

Is it possible to check in background if an android/iOS device is in use at the moment?

Is it possible to determine if a device (non-rooted) is in use at the moment, even if my app is not in the foreground? Precisely "in use" means the user made touch events in the last 5 seconds or display is on.
If so, what specific rights are required?
Thanks
AFAIK, android security model would not allow you to record touches if your app in not in the foreground.
There are some crude workarounds like overlaying a transparent screen to record touches. Not sure if these work now though.
"in use" means the user made touch events in the last 5 seconds
In Android, that's not practical, short of writing your own custom ROM.
or display is on
In Android, you can find out if the device is in an "interactive" mode or not. This does not strictly align with screen-on/screen-off, as the whole notion of screen-on/screen-off has pretty much fallen by the wayside.

How to change screen dimming time programatically in android?

I would like to change system dimming time in my custom application.
My device is set 30s-timeout for sleep.
So if I am in home screen and leave it, it goes dim in 23sec and screen is turned off in 30sec.
Is there any idea for changing screen dimming time for my application?
It's ok to change framework source.
Thanks in advance.
I don't believe android let's you control this option from an application. Of course it lets you set a wake lock for applications to keep the screen on, but I'm pretty sure the screen dimming or "Backlight Timeout" can only be set by the OS.
I don't think there's a programmatic way of modifying the system setting, but you can work around it if the current screen timeout is known...
Figure out how long you want to keep the screen on for and then subtract the current timeout seconds from that time span. The resulting number is how long you should programmatically keep it on via something like View.keepScreenOn

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