My app keeps screen on - android

My android app is able to run on the bakground and I'd like it to do so wenever the user stops interacting with the phone.
I the app is open and the user does nothing with it for x time, I expected Android to lock the screen (which is what i want).
However, the app simply keep th screen on. Why?
I do not use android:keepScreenOn="true" anywhere on the app.

Are you acquiring a WakeLock ?
If yes you should use PARTIAL_WAKE_LOCK instead FULL_WAKE_LOCK or SCREEN_BRIGHT_WAKE_LOCK the first lets the screen go off, the others don't

Well, turns out it was my fault, not the apps.
I had enabled 'Keep screen awake while charging', and since I had the phone connected to the computer, the screen never locked...
I'll leave this here in case someone else is in the same situation.

Related

Prevent my app to sleep when device goes to sleep mode

I have written an app that should be running continually event if device goes to sleep mode.I have tried many things like services (with startForground also) , alarm manager and persistant="true", but when device go to sleep (by user or by device) , my app is terminated.and even PowerManager does not work too.
There is an option in device setting that is 'Keep running after screen off' (in huawei device : setting > app > my app options > battery > Keep running after screen off). So when this option is active , the app is running for ever.I want implement something like this in my app programitically.
I saw some apps that implemented this , but I dont know how.
So how can I do that?
Use ForgroundService.
If you check your device applications with an app manager, you can see that some of them using a sevice that is always running. even if you stop them, some of them start working again.
see more about services behavior in android developer.
and search for "keeping a service alive".
On some devices (Huawei, Xiaomi, LG) there is a special setting that contains a list of apps that are allowed to run in the background. It sounds like you are talking about trying to accomplish the same thing. This cannot be done on those devices. On those devices, unless your app is added to the list of apps that are allowed to run in the background ("Protected Apps" on Huawei), the Android framework will not restart your app if it is shutdown. There is no way for you to programatically add your app to this list, and there is no way for you to work around this feature.
Certain apps need to keep the screen turned on, such as games or movie apps. The best way to do this is to use the FLAG_KEEP_SCREEN_ON in your activity (and only in an activity, never in a service or other app component).
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Let screen off after releasing a wakelock with ACQUIRE_CAUSES_WAKEUP flag

Context: I'm under Android 4.0.4.
The code I'm stating is inside an Activity.
The application is a Cordova application, I'm writing this code inside a plugin.
I think I read all the questions here in StackOverflow and the Android docs.
I want to acquire a wakelock, wake up the device, do some stuff and then, when the wakelock is released, i want the screen to turn off again and let the phone sleep.
I have tried several approaches:
I tried several examples found here to wake up the device.
The only way I managed to turn the device on programatically was aquiring a wakelock with the ACQUIRE_CAUSES_WAKEUP flag. This works fine. The problem comes when I release the wakelock. There's no way I can turn the screen off again. It just stays on forever. I tried with all the WAKELOCKS available and with all the FLAGS available.
Note:I'm not totally happy with this approach because as stated in the docs, the only wakelock that is not deprecated is the partial wakelock, which unfortunately can't be used with this flag.
As I couldn't find any way of letting the screen off using the wakelocks functionality I tried to turn it off programmatically.
I think I tried all the examples found here to turn the screen off, and the only one which worked was:
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);
The problem is that using this method I can't turn it on again. When I acquire the wakelock again it just stays off.
Also, when I try to wake up the phone pressing the "home" button it doesnt wake up. It only responds after pressing 3 or 4 times the "power" button. Seems like this approach freezes the phone.
I tried to turn on the screen programmatically before acquiring (and before too) the wakelock, restoring the default brightness value using the same code as above, but It doesn't work.
It's almost a week that I'm struggling with this. Any piece of advice will be appreciated.

Detect Volume Button Press when Screen off

I have a service that detects when the display of my Android phone is turned on or off. When turned off, the service calls an activity that uses dispatchKeyEvent to detect when the volume up button has been pressed. Unfortunately, apparently the activity can't do this when the screen is off. (See this post.)
I've noticed, though, that something at some level is detecting that event, since the following LogCat message appears when I press the volume-up button when the screen is off: "CatService: Return current sInstance". The message seems to be device specific, since on a different device something different appears in the log, but I'm really only concerned about the first device.
I've done some research into CatService, but haven't found much and can't figure out how I might be able to use it, or whatever is generating the log message, to detect the volume up button press. I'd appreciate any light that anyone can shed on this.
The only way to keep detecting things like this when the screen is off is to acquire a WakeLock that will allow the screen to turn off, and still let your app function. However, this drains the battery life quite a bit, and should only be used when absolutely necessary.
In this case, you will need a PARTIAL_WAKE_LOCK.

Capturing touch events even while system is in sleep

I'm developing an app which requires the system to get the touch events even after the system goes to sleep mode or after the user locks the screen, I tried searching for a solution but it is hard to find one.
Should I want to set any permissions or is there any inbuilt methods or can I override any methods to perform this functionality.
I'm developing an app which requires the system to get the touch events even after the system goes to sleep mode or after the user locks the screen
Fortunately, this is not possible. Otherwise, the device would not be asleep, and battery life would suffer as a result.
Here is a link that shows how to prevent the phone from sleeping.
If you couple that with, say a black screen to 'pretend' the hone is sleeping but actually running your code. So your code can still intercept touch event
Then you need to install your app as a service and make it start when the device is turned on.
You will not need NDK or rooted device for that (sorry, got a short night :) )

How do I force screen on (if it's sleeping), in my Activity?

I am writing an Android alarm-like app.
I want to let the user to choose if to always keep the screen on for the whole application duration, or if she want it to go to sleep according to her device power manager settings.
The first scenario is correctly handled with this code:
getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
But - in the second scenario - when the screen has been swichted off by power manager, I can play a sound via AudioManager, but I can't force the screen to switch on...
I'm using Build.VERSION.SDK = 10 and testing on a Samsung device with Android 2.3.4.
You can maintain a WakeLock, and acquire the WakeLock when you want to turn the screen on.
You can check out WakeLock here.

Categories

Resources