Android device sleeping issue - android

I have a little issue when I set option for device not sleeping. I'm connecting to web server and downloading data and when I'm doing this I need the device stay awake. I'm using this in this way :
1.In my Synchronization class when I start a connection to a web server I set this :
PowerManager powerMan = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = powerMan.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
and onDestroy() method in this activity I set :
wl.release();
But it seems that after this the device is not sleeping even if I close my application. Is there something that I'm doing wrong. Thanks in advance!

Try to use this instead of WakeLock Manager :
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
this will keep your screen on and you can remove that option by doing his :
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
This should help.

Related

turn on screen from service

I have Android 7 (API 24). I launch my app which launches started service. Then timeout causes screen to turn off. The service does something in background and eventually wants to turn the screen back on. How can I turn the screen on from service?
I already tried:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "MyApp::MyWakelockTag");
wl.acquire();
The permission for WAKE_LOCK was set. I also wanted to use FULL_WAKE_LOCK flag, but this is deprecated. Anyway, this doesn't work.
I also tried to launch my activity from service and implement this in OnResume of my activity:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
This also doesn't work (i.e. it doesn't turn the screen on).
Thank you for your help.

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.

Detect phone movement when screen is off

I am developing an application which should detect user movement and when he stops for more than 5 minutes it should sound an alarm. I was able to detect movement with accelerometer but the problem is it doesnt work when the screen is off. I have also tried using partial wakeLock. Re-registering accelerometer doesnt work either (this should be workaround for motorola devices).
Maybe I can do this using GPS and sound an alarm when GPS speed is less than 1m/s for more than 5 minutes but I am not sure if I will receive GPS updates when screen is off.
So I need a solution that will detect user movement even is screen is off on most devices. Any ideas on how to acomplish this?
Thanks in forward
You should acquire a partial wake lock for this kind of operation. Use the PowerManager class.
Something like this:
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
PowerManager.WakeLock lock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SensorRead");
lock.acquire();
You need also this permission in the AndroidManifest.xml:
<uses-permission android:name="android.permission.WAKE_LOCK" />
Is recommendable using lock.release(); when you're done your work.
EDIT:
Also, this article could be useful for you.
partial wake lock this is what you need to access while your screen is off.
private PowerManager.WakeLock mWakeLock;
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
mWakeLock.acquire();
And after you're done, just release the lock:
mWakeLock.release();
If you obtain accelerometer data in a Service, you could simply acquire lock in it's onCreate() and release in onDestroy().

How to avoid user turn off the screen which result in the wifi turned off during downloading

In my application, I use AsyncTask to download large files (300M+). I noticed that when the user turns off their screen (locks their device), the wifi will disconnect and the download will hang.
I wonder if it is possible to avoid this?
You required to implement WakeLock in your application. Wakelock will wake up the CPU incase of screen is off and performs the operations in normal ways.
Write down following code before starting the AsyncTask,
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "bbbb");
wl.acquire();
You need to write wl.release(); on PostExecution() method. And you need to define permission in AndroidManifest.xml as follows,
<uses-permission android:name="android.permission.WAKE_LOCK" />
The common practice in this case is to take a WakeLock to keep the CPU awake, and take a WifiLock to keep the wifi connected, so that your app keeps running even if the screen is turned off.
Don't forget to release the locks when you're done!

Running a Android App while the screen is black

I'm trying to run a android app while the screen is black, i used a service and i tryed to add also a WakeLock of the type "PARTIAL_WAKE_LOCK".
The service play a song and save data from the accelerometer to the db, but when i press the red phone button to make the screen black, it play the song but it don't save data to the db. Have anyone a idea of why it don't save data to the db?
Tnk's
Valerio
With this you ensure that the code between acquire() and release() are executed even if the phone is in stanby mode, since the cpu will stay active.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
..CPU will stay on during this section..
wl.release();

Categories

Resources