I hace attached my device to the PC and run my service via Eclipse on the device. Now I would like to see what is the behavior of my service over power-off and power-on. The problem is that once O power-off the device my service is lost. Is it possible to keep the application persistent on the device over power-off?
Thanks.
Your service will not run when the device is powered off - because well there is not power. But you can make the service available as soon as power is provided to the device. Make sure your service is efficient so it is not killed when memory is getting tight. Then just use a broadcast receiver to catch there
<action android:name="android.intent.action.BOOT_COMPLETED"/>
intent. From the receiver you can send and intent to launch your service. You could also use this intent in your service's Intent Filter to start the service. Use the PowerManager with a partial wake lock to help your service run when the screen is off - But note: This feature does not work on all devices - Of all the devices I have used this feature works on half, but not perfect.
If you want to help your service from not being killed by the OS even more - run it as a foreground service using the startForeground() API call in the service object.
instead start it again using broadcaste that is sent when device boots.. see here and here for more..
Related
I have built an android app for collecting mobile and wearable sensor data. I want this app to be running in background 24/7 without being killed by OS. I am aware of all the pro and cons of running it 24/7 but that's my main business requirement.
EDIT: I have made it as foreground service and it works as long as I keep interacting with my phone but if I keep it idle for let's say 4-5 hrs OS kill it despite being listed as foreground service
It's a pain to run a background service after Android Marshmallow.
Doze and StandBy Modes halt all background operations.
Even if you use job dispatcher or work manager, the minimum interval for running an operation is 15 minutes, even if you set it to less than that.
So you need to start a foreground service with sticky notifications to do your work. You can take a look at this article to learn how to start working with foreground services.
don't forget to put those permissions into your manifest file if your app is targeting android pie
and you can rerun the service on phone restart by using a broadcast receiver
which listen to this action
<action android:name="android.intent.action.BOOT_COMPLETED" />
and also you can stop it by listening to this action
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
Use Service for collecting data
you can run it foreground and it will not be killed by OS
Make the application into a launcher and set it as a default launcher. You can follow this guide here. The idea is the launcher is launched by the OS when the phone boot and it is never killed when system runs out of memory.
I am trying to create an app where there would be multiple alarm(according to the will of user,so not definite ) but the i am unable to find any assistance. I also my app to run even if the phone is restarted. so in short i want to know about the operations of the usual alarm app that runs in the android phone
You can use a BroadcastReceiver to start your service when your phone restarts. In your service then you can register your alarms.
Here you can find an example:
Autostart Service on Device Boot
I want to create a service for my wearable that runs in background that will detect a Bluetooth device. I have written code that is able to detect the Bluetooth device but it uses Activity - onCreate() method to start scanning.
I read many articles on the internet about writing services that runs in background but in every example the service is started from the Activity. I want my service to be running without starting from any Activity. Is this possible? I mean when I install my app is it possible for the service to start running automatically?
Seems like there is no way of starting a service without starting the Activity. I got the answer from there How to start android service on installation
I am making an Android app which is supposed to monitor the other running apps.
The question is, how do I make my app run continuously from when i first activate it. It should also start running by itself when I switch off and reboot the phone.
Any suggestions on how to ensure this? I am considering using a background thread but I am not convinced this alone will suffice.
You should make an android service that running continuusly in background
http://developer.android.com/guide/components/services.html
http://www.vogella.com/tutorials/AndroidServices/article.html
http://www.tutorialspoint.com/android/android_services.htm
Well you should try to have an Service into your app that could keep track of the other App's running
So Every Time the App start's the Service Would Start And Accordingly give The Updated to your app regarding the Other App Working
For Boot Time Start you Should Try System BroadCast Receiver
You should Really Follow this Tutorial
Create a service to monitor apps in the background.
http://developer.android.com/guide/components/services.html
To make the app load at boot:
Create a BroadcastReceiver and register it to receive ACTION_BOOT_COMPLETED. You also need RECEIVE_BOOT_COMPLETED permission.
Refer to this:
Android -Starting Service at Boot Time
You have to use a Service. But keep in mind that your service can be killed and restarted after a while.
And you should register a broadcast receiver to listen for ACTION_BOOT_COMPLETED so that you can start the service once the phone booted successfully.
I want to know the difference between services and broadcast receivers, can anyone point out an example that can be observed on android mobile devices.
Thanks
Service: If you want to do something in background , this will be running always in background even if the application closed. You can create this in separate process and also you can give your service to other app if you want. Downloading any content or Music is good example
Broadcast Reciever: Usually system will send some info which can be recieved by your app if you would wish to ,by registering. And you can do something what you want when that thing happens by using onReceive method.
Example is the system will send BroadcastReceiver when new sms arrives or Booting done
Here is good article : Service and BroadcastReceiver
Service is used when you want to do something in background, any long running process can be done using Service in Background. For example, you want to play music when your application gets close. In that case service will be running in background with music.
Example of Service
BroadcastReceiver is used when you want to fire some stuff or code during some event. For example, event can be on Boot of Device. If you want to perform something when device Boots, date and time changed etc...
Example of BroadcastReceiver
I think of it possibly a different way. A Service receives intents that were sent specifically to your application, just like an Activity. A Broadcast Receiver receives intents that were broadcast system-wide to all apps installed on the device.
(The reason I say a Service is a bit like an Activity is that: You wouldn't broadcast a message saying "start Activity MyActivity" across all apps installed on the device. It is only for your specific app.)
Of course, as others mentioned, a Service can continue running in the background, whereas a Broadcast Receiver should finish quickly (e.g. if it is running for more than 5 seconds it may be killed by the OS). The Broadcast Receiver can still run in the background (when app is closed) under certain circumstances. For this, it's worth mentioning that there are actually two types of Broadcast Receivers - Manifest-declared, and Context-registered. They have different lifespans and restrictions - the former can receive broadcasts in the background with certain restrictions, while the latter cannot receive broadcasts in the background (app must be running and active) but has no restrictions on the types of intents that can be received.
Both services and broadcast receivers must be specifically invoked (via an intent), but for services this is usually a specific call (e.g. when your app is started or when the user clicks some button) whereas for broadcast receivers they don't need to be explicitly started as they will start anyway when a relevant broadcast is made.
Here's how I would think of it:
Type
Displays UI?
Can continue running for a long time when app is closed?
Can receive intents when app is closed?
Intents must specifically target your app?
Restricted list of intents that can be specified?
Activity
Yes
No
Yes
Yes
No
Service
No
Yes
Yes
Yes
No
Manifest-declared Broadcast Receiver
No
No
Yes
No
Yes1
Context-registered Broadcast Receiver
No
No
No
No
No
1: Only if you target Android 8.0 or above. The restrictions are not applied if the intent specifically targets your app. The restricted list of intents can be found here.