Android - Disable proximity sensor in my app - android

I'm working on an app that allows the user to start a phone call from the app itself and once the call was initiated, the user can get back to the app to do some UI actions.
My current problem is that once the call started the proximity sensor was enabled which makes it almost impossible to work with the app (depends on the sensitivity of the sensor - per device).
Is there anyway to control the sensor?
For example, I would like to disable the sensor once my activity is visible and re-enable it once invisible.
Thanks,
Lior.

Try to set WakeLock in your application code...like below....
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock( PowerManager.FULL_WAKE_LOCK, "FullWakeLock" );
Hope it will help you...

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 keep awake my android device

Right now my app has to stream music. I'm using a web server and everything works if the phone is awake or is plugged in to a power source.
But I really need to continue streaming until the phone go to sleep. I'm using PowerManager PowerManager.WakeLock in order to keep the phone awake.
This is part of my code:
pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wl.setReferenceCounted(true);
After that I have the wl.acquire(); and the release. I have a handler that does the release 5 minutes after the songs stop streaming.
int timeToWait = 300000;
turnOffDevice.sendEmptyMessageDelayed(0, timeToWait);
But it works for a short period of time then the song starts jumping and most of time the songs stop.
Hi I've already fix my problem, the problem was in the wifi, it was sleeping after some time so I added a wifilock and in the constructor of the class in I'm using WIFI_MODE_FULL_HIGH_PERF this flag avoid the device turn off the wifi
wifilock = manager.createWifiLock(3,TAG);// WIFI_MODE_FULL_HIGH_PERF
The only problem is this flag is only available since API 12, but for now is not a big deal into my project. If you want more information in this linkWIFI_MODE_FULL_HIGH_PERF

Preventing application/screen timeout Android

I have an Android (version 1.5) application which needs to be constantly running when a button is pressed. So, when a Button is pressed I would like the phone to remain on and not want the screen or CPU to time-out.
When another Button is pressed I would like the phone to be back to normal and time-out as per user settings.
Update: As suggested by Steve Pomeroy, this might be a better way to do it.
You can use a WakeLock that requires the following permission:
<uses-permission android:name="android.permission.WAKE_LOCK" />
Here is how you aquire and release a WakeLock:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
// wake locked...
wl.release();
Depending on your requirements you might be able to use a different type of WakeLock.
Instead of using a wakelock, you should consider the solution proposed here: Force Screen On
It is much easier to use and doesn't have the potential of accidentally wasting the user's batteries.

Categories

Resources