I'm trying to create a service that run in background in android.. That service should work only when the device is on sleep (Means that screen is off)..
I think that can be done using a service but I don't know how to detect if the phone is awake or not.. Any Idea ? Thanks..
For screen on-off state, you can try with ACTION_SCREEN_ON and ACTION_SCREEN_OFF intents, as shown in this blog post:
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
From here : How to detect whether screen is on or off if API level is 4?
Related
One of the activity in my app connects to bluetooth scanner and get the input and display it in a listview. I got that working by following the bluetooth chat sample app. now the issue is, whenever screen is locked, or device goes to powersave mode, my connection with bluetooth scanner is getting disconnected. how can continue to keep the connection stays on until app is really closed from the system? I am new to Android . I did some search it says to use wakeful lock but how can i do that? Some samples would be much appreciated! Thanks for your time.
Instead of using wakeful lock, I would recommend you to implement the connection inside a background service.
The service runs until its killed by the system or you stop it.
You can found information including a simple example here:
http://developer.android.com/guide/components/bound-services.html
I can't find a list of events that can be listened for while a device is sleeping.
For example, it is possible to use LocationListener while asleep.
My goal is to detect when a user is in his car by at least one of the
following methods, which will then wake the phone:
NFC tag on car dock
Bluetooth-enabled car radio in range
But failing those, I'm open to ideas.
Thanks
The best idea so far is to wake the phone periodically as per this link:
Android - periodically wake up from standby mode?
As per the documentation, I could wake the phone up and just use the CPU, while keeping the screen off.
But there might be disadvantages, perhaps there is a better way.
You should create a brodcast receiver and register specific actions.
An example to detect a boot: Android -Starting Service at Boot Time.
This requirement is only satisfied if the app is running in the background.And if the Screen is turned on if the user presses to check any notifications then an Asynctask is called if the app is running in the background and makes a call to the server.
I have tried using Broadcast Receiver when screen on and tried to execute, it works only if the app is on the front screen after pressing the home button .And then if the user presses Power button after an hour then nothing happens .
Basically I am not sure if the app is being killed after sometime when in background. Please help me.I am a noob in Android and this functionality is something I thought most of the developers might be using but I did not see anything except service calls and I really did not want any service/alarm-manager as I don't want it to work continuously.
TIA
how to make server call whenever device screen is turned ON without Service
This is not possible. ACTION_SCREEN_ON is a broadcast that can only be received by a BroadcastReceiver registered via registerReceiver(). So, unless you are the foreground activity, the only way you can receive this broadcast is via an always-running service, which is not a good idea.
as I don't want it to work continuously
Then do not "make server call whenever device screen is turned ON". Find some other solution for whatever business problem you have that you are trying to solve this way.
How do I get the status of android phone when the lockscreen start to countdown disabled lock?
I know I need some services to use this and even BroadcastListener, and I will put my condition on BroadcastListener.
Sample code:
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction(){
}
is there an (android.intent.action.) too that I can get the lockscreen status?
You have two options:
See the broadcast intents ACTION_SCREEN_ON and ACTION_SCREEN_OFF.
For Android 2.1 and higher you can use isScreenOn() of the PowerManager.
I am an android developer I have a problem..
I want to be get notified when the phone screen goes to sleep
PLease Help wiith a sample code.
Set up a BroadcastReceiver to watch for ACTION_SCREEN_OFF broadcast Intents.