Detect phone movement when screen is off - android

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().

Related

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!

Android - Disable proximity sensor in my app

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...

Keep My App alive - Android

I'm trying to make an App on Android 2.2 that has to run whether the screen is on or off.
I tried realizing it via a Service, but when my phone turns the screen off the service stopps working and I don't understand why.
In my Application I use
startService(new Intent(this, MyService.class));
when the User presses a button
and
Notification notification = new Notification();
startForeground(1, notification);
in the onCreate-Method of the Service class
(I tried it within the onStart() Method, too)
(I also could not find out what the id-field of startForeground() expects so I used the 1)
The service then should start an infinite vibration pattern of the phone so I know whether it is running or not.
But when I turn off the screen, the phone stops vibration immediately
Please help me. I don't know how I can fix that (and google was not a big help)
Sincerely
zed
Android devices go to a sleep mode when idle (e.g. when the screen is off), in order to conserve the battery.
To keep your service running you need to aquire a WakeLock. There are plenty of tutorials how to use it, like this one.
Note that having a service running all the time will drain your battery. Also make absolutely sure to release the wakelock when not needed, otherwise you're phone will always be awake.
Try returning 'START_STICKY' from 'onStartCommand() '. This will keep the service running, restarting it if necessary, without keeping the screen on.
Have a look at http://developer.android.com/reference/android/app/Service.html#START_STICKY
try this
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
and in manifest file
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
so when your notification or alarm manager service running, screen will light on and the keyguard will unlock.I wish it works for who has this problem too.

Method OnSensorChanged() when screen is lock

On my Samsung Galaxy S 2 I have the known bug when OnSensorChanged() method does not call when screen is lock (http://code.google.com/p/android/issues/detail?id=3708 ). Is there any solution for this?
You solve the problem by getting a partial wake lock from the power manager which keeps the CPU from sleeping when the screen goes off.
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock lock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SensorRead");
lock.acquire();
Make sure to release it when you are done so you don't drain the battery.
You need this permission in your manifest too:
<uses-permission android:name="android.permission.WAKE_LOCK" />

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