Unable to Keep Screen from Sleeping in Android 4.2 - android

I have a simple battery test app I wrote for Android and have used for several versions now successfully. It works fine in Android 4.1 The app simply launches the chrome browser to a different web site, logs the time to a database, waits 60 seconds and does it all over again.
Now, in Android 4.2, the screen turns off and system goes to sleep despite the fact that I have a wakelock and have set screen timeout to never using settings. What's most annoying is it turns off at an indeterminate time, not a fixed time after starting the test. Any idea what I need to do?
Here's the wakelock code I call in the service that launches the browser over and over again.
super.onCreate();
// Wakelock
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
I also put this in the layout file
android:keepScreenOn="true"
However, the app's screen itself isn't visible throughout the test as Chrome is always the foreground window and the browser launcher is a service and the wakelock is in the service.
Any idea how to keep the screen from timing out? Nothing seems to work.

Just add following line to your XML file......
android:keepScreenOn="true"
:)

I would guess that Android is killing your service and causing your wakelock to be released. Try making your service run in the foreground (with startForeground()) and see if that helps.

Related

Android prevent screen timeout

I got the following problem,
I have an unity Game which I would like to run.
During running the Game, it always lowers screen lighting and switch of after about 2,5 minutes.
I already tried:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
and
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
Links:
Android disable screen timeout while app is running
Android Screen Timeout
Programmatically disable screen timeout
But after this, it still switches off as before.
Were there any changes done, in android or are there any other solutions to solve my problem.
Thanks.
Solution was very simple done using Unity adding the following Code into the Start() Function:
Screen.sleepTimeout = SleepTimeout.NeverSleep;
I doesn't know if it is switched of automatically by Unity or nor but I hope so.
Link to Unity Documentation

How to keep Android awake in background when screen is off

I runs web server in my rooted Android Tablet. I setup it for web development. I created a home network by this Android server. but when the screen of the tab turns off, the server also stop working & again start working when i turn on the screen. but its not possible to turn on the screen for long time. I can be harmful for my tablet. Is there any way to keep awake my tablet when the screen is turn off so that my server can work properly in Background.
please help
You can run a service in the background and acquire lock like this :
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
and return onStartCommand START_STICKY. To release the wake lock, call wakelock.release().
Do not forget to put the permission in the manifest file :
<uses-permission android:name="android.permission.WAKE_LOCK" />
Or you can use this app to prevent phone from sleeping :
https://play.google.com/store/apps/details?id=nl.syntaxa.caffeine

How to make my avd stay awake

Problem :
I am testing android application using robotium, My Problem is screen gets off after few second if user did not interact with the app,how to keep screen always on for testing app?
What I tried :
Made Stay awake on under development options of avd
I cross verified this post but it was related to development side where i cant modify the source of apk
My Question :
how to keep screen always on for avd testing app ?
For testing you can instantiate a WakeLock.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(pm.SCREEN_DIM_WAKE_LOCK, "My wakelook");
wakeLock.acquire();
Don't forget the permisson WakeLock
<uses-permission android:name="android.permission.WAKE_LOCK" />
Alternatively you can try the "wake lock" option in the devices development settings.
Window w = ((Activity)mContext).getWindow(); // in Activity's onCreate() for instance
w.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );

Android Wakelocks in 4.2 for flashing the screen on and off

In versions of android before 4.2 I used to have some code to flash the screen on when a notification came into the app (if users wanted it to flash). I used:
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP, "example_tag"
);
wl.acquire();
//this would switch the screen on
//then after a little while I'd call
wl.release();
So the "aquire" method would switch the screen on (if it wasn't already) and the "release" would switch it straight back off.
In android 4.2 it seems that the switching off doesn't happen when you release the wakelock, but only when the screen timeout setting for the display is reached (default seems to be 2 minutes)
Is there any way to make it switch off when the wakelock is released. I've seen some bits in the API about specifically switching the screen on and off, but I'm a little unsure about using as I wouldn't want the app switching the screen off if somebody was in the middle of doing something, so the wakelock seemed to work well. Any suggestions?
The only way I've found it to set the screen timeout down to a short time but store off the default and restore it when finished. This will turn it down to around 5-7 seconds at the shortest and you have to be careful to restore the setting back correctly afterwards as it's a total hack really.

How to modify Wi-Fi Sleep policy programmatically?

I want to keep Wi-Fi enabled when device goes to sleep mode, I tried several options, but nothing has worked out like acquire Wi-Fi lock and set wi-fi enabled. These options are not working then I tried with wake lock, this is working perfectly when my application running in the foreground, but when application is running in background, after some time excess wake lock error comes and application is getting destroyed and on top of that I can't use wake lock all the time because it dried out the battery.actual requirement is my application should run 24/7 and connectivity with the server always stays on because server can send data any time, but when device goes to sleep mode wi-fi is getting turned off so I need to set the wi-fi sleep policy to never upon starting of my application and will set back to normal policy when application gets destroyed. I tried following code in my main activity and runs the application and allow device to go to sleep mode and after some time connection is still getting closed:
Settings.System.putInt(getContentResolver(),
Settings.System.WIFI_SLEEP_POLICY,
Settings.System.WIFI_SLEEP_POLICY_NEVER);
So please do help me to resolve this issue.
Use import static android.provider.Settings.System.WIFI_SLEEP_POLICY; instead of the name string Settings.System.WIFI_SLEEP_POLICY.
It works perfectly.
In my case Settings.System.WIFI_SLEEP_POLICY does not work. I was able to keep wi-fi on with the PowerManager
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
Then in the OnDestroy() I call
wl.release();

Categories

Resources