Wakelock works only when screen turned on - android

many things has been written about wakelocks in Android. I'm using wakelock this way:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
partialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Partial");
partialWakeLock.acquire();
// few hours work
partialWakeLock.release?
In most of devices, PARTIAL_WAKE_LOCK can keep device running whole night. But there are few models (Samsung Galaxy ACE 2 (i8190) - 4.1.2 etc.), which ignore this kind of wakelock. Wakelock is held, but phone goes to sleep if device's screen is turned off.
Is there some way how to determine, if application is running in device with this problem? I can let user turn devices's screen off for a few seconds and check if work has been done, but is there some automatic way?
Thanks in advance.

Related

Battery optimization not available on WearOS

I'm developing a tracking application that runs on Wear OS watches. I have some troubles when the watch goes to sleep. As soon as the watch goes to Deep Idle mode, all sensors acquisition stops. For that I'll need to whitelist the app using:
Intent intent = new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
context.startActivity(intent);
This piece of code works well on mobile version but nothing happen on Wear OS version. I realized that Wear OS as no UI to set the battery optimization mode which should be the cause of the fact that nothing happens when trying to change the optimization.
I also used a wake lock to keep the CPU partially awake with:
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MY-WAKELOCK");
But even with that the watch goes to deep sleep after about 15min ?
The last solution I see is to force the screen on or at least dimmed but it will drain the battery very fast.
Is there another solution to keep sensors working ? How can I whitelist my app on a Wear OS device as no UI seems available ? As anyone already encountered this issue?

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

Android Device screen not turning off

I started songs on one of the music apps, on my android device.
Although I can see only partial wake_locks being acquired, still device display doesn't turn off even after screen timeout.
I wonder what is keeping the device screen on.
Can someone suggest where to look for probable cause.
Thanks!
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(
pm.SCREEN_DIM_WAKE_LOCK, "My wakelook");
// This will make the screen and power stay on
// This will release the wakelook after 1000 ms
wakeLock.acquire(1000);
// Alternative you can request and / or release the wakelook via:
// wakeLock.acquire(); wakeLock.release();
I used this code and works fine to me.

Make app/service "run after screen turns off" ? -Android-

i a'm completly android programing noob
i'am working on developing "Androrat" (this project is not mine i had found it over the internet)
when the screen turns off the connection is lost
how can i make the processes/service always running ?
I had googled some and found that i have to include a partial_wake_timer
I want the code and where i have to enter it ?
I a'm completely noob please bair with me
http://dl.dropbox.com/u/102520949/Untitled.jpg
or maybe a kind person can help me over team-viewer ?
A partial WakeLock is what you want. It will hold the CPU open, even if the screen is off.
To acquire:
PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
To release:
wakeLock.release();
This answer was taken from this link:
How can I keep my Android service running when the screen is turned off?
Next time, please search more deeply.

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.

Categories

Resources