I have a background Service that control with sensor accelerometer when the phone is upside down and if it's true phone sound. If I run the service and put my phone upside down it's ok but if I push power button and able a lock screen the phone not sound. I don't understand if the service is stopped or is the sensorchangelistener. i have also used into onCreate the wakelock.acquire method but it work good with my phone (galaxy s) but not work with phone of my friend (motorolo defy).. Can you help me? :-(
It's not possible to use sensors while the phone is in standby, or even with a partial wake lock. You need a full wake lock to keep receiving sensor updates. Supposedly some phones have a workaround, in that once the phone goes into standby you can request sensor updates again, but this only works for a few phones, and is hardly a reliable solution. So in short, the only way is to keep the phone awake.
Related
I am currently working on an application which requires a service running in the background to play music when the phone is in sleep mode.
I would like to know what are the actions that can be performed to trigger play music action while the device is in sleep mode.
Basically when your phone is in sleep mode it shuts down the CPU, when it is in sleep mode only thing which works is GSM or CDMA radio that is your incoming calls, SMSs, IP packets and AlarmManager...
After this answer you would think that, It even stop the services also if the phone is in sleep mode.
Then my Answer is, Yes. it does, since the CPU is powered down, all processes freeze in place. For more information you can visit this link.
Thanks to #CommonsWare for great answer.
There are 3 states an Android phone can be in: Awake, Asleep and Off. See: Android: Sleep stages/levels on an Android device?
In sleep mode screen goes off, CPU shuts down along with Wi-Fi and GPS radios. See: http://developer.android.com/reference/android/os/PowerManager.html and Android Sleep/Standby Mode
But GSM or CDMA radio still works and the device can receive incoming calls, SMSes, and IP packets. Also, Google introduced new Google Cloud Messaging service that sends data to devices and can, as I understood, wake the device if necessary. See: http://developer.android.com/guide/google/gcm/index.html
So, my question is: can application on Android device be updated via Google Play if the device is in sleep mode? I mean is Google Play allowed to receive some kind of messages from the cloud, wake the device and update applications? I assume that the application is allowed to be updated automatically and can be updated through mobile network (3G/4G).
Thank you.
UPDATE: I did some research and it happens that Nippey's answer and comments are correct. I did not see any updates to wake my device but as soon as the device is awake updating mechanism starts to work normally. So, theoretically, Google Play Store can start updating your applications right after the device wake up.
The device is able to process push-messages while in sleep mode.
As soon as you trigger an update via the play.google.com, this will issue a push request, which, as a consequence will wake up your device. The update will be installed immediately.
So, it doesn't work in sleep mode, but if the device gets a trigger to wake up, it will do what has to be done.
I have an application that needs to continuously listen for incoming requests over wifi. A service that runs in the background does this job. However, this service falls asleep after a while when the screen turns off.
The solution from what I have searched is to use AlarmManager to keep it awake. But it is said that this will drain the battery of the device.
So, is there another way to do this?
For eg, what do apps like Whatsapp and Skype do? They don't seem to kill too much battery but they have continuously running services right?
Also, in case AlarmManager is the only way, it would be really kind if someone could share a tutorial or example for it.
The solution from what I have searched is to use AlarmManager to keep it awake
That will not help. Once the device falls asleep, your socket connection will be terminated. You would need to use a partial WakeLock plus a WifiLock to keep the device powered on continuously.
But it is said that this will drain the battery of the device.
The WakeLock and WifiLock will definitely drain the battery.
So, is there another way to do this?
Not if you need to use WiFi.
For eg, what do apps like Whatsapp and Skype do?
They do not use WiFi when the device wants to go to sleep. Once the WiFi radio powers down, they use mobile data, so no WifiLock is needed. For mobile data, incoming packets will wake up the device, so you only need a WakeLock while you are actually doing work, rather than constantly.
The best answer is to switch to use C2DM, though.
Actually its not your service which falls to sleep, its your WiFi unit on the device. Manufacturers like HTC (or perhaps all Android devices) have implemented this kind of behavior on their devices in which the WiFi unit goes standby after certain time period of screen-off. This helps the devices to save battery when its not being used.
I have develop one android application which will talk to server 24*7 over WiFi but when phone goes to sleep mode it stops talking to server means socket is getting closed so for resolving this I added code to acquire partial wake lock in service onCreate() and release it on OnDestroy() method o service so even though phone goes to sleep mode my application can talk to the server.
Problem is: If you keep the device idle for longer period (more than 8 hours ), it stops communicating with the server and WiFi turned off. I heard about deep sleep mode of device, in this case it will shutdown CPU,WiFi etc.. so how to restrict it for getting CPU and Wifi turned off?
Please help me with some sample example.
Regards,
Piks
I assume you have tried full wake lock as well?
In my application I want to detect the shake event and I'm using SensorEventListener, the code is working fine when my activity is running in foreground. But when I press the lock button of the phone to lock the screen, the shake event can't be detected. I have tested my code on the Samsung gts5360. But the same code is working fine on sony ericssion xperia mini pro. Actually my Samsung phone is not detecting the shake events when I leave the device idle for approx. 45 seconds, after locking the device.
Then I shake the phone, it does not detect the shake, but when I shake phone after several seconds delay it starts listening the shake. This behavior of my samsung phone is not consistent. It starts and stop listening the shake event after a random amount of time.
Now my question is that "Is this the android feature that the device does not detects a shake event when the screen is lock/Off ?".
If it is, then how my samsung phone starts/stops listening after some seconds of locking the phone?
And how it is continuously listening the shake event when screen is lock/off in "Sony ericssion xperia mini pro"?.
Is this feature vary vendor to vendor?
If some body needs my code then let me know,I will provide it.
The problem is for a long time there has been no consistent standard for what to do with sensors when the screen goes off. Some devices allow it to keep working and others do not. Eventually the Android team decided that for it to work an application should acquire a partial wake lock for this kind of operation:
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
PowerManager.WakeLock lock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SensorRead");
lock.acquire();
This permission is needed: "android.permission.WAKE_LOCK"
You need to make sure you release the wake lock when you are done with it so the CPU can go fully to sleep.
Even with all of this it may not work. I've found that LG phones most recently are less likely to support background sensors. Also many Motorola phones don't require a wake lock but instead just need to re-register for the sensor when the screen goes off.