Once the user gets into an activity, I need to send a value to the Web Service and, once s/he exits the activity, I need to send another value to the server. It needs to happen at that exactly moment.
Android Developers guide says I should use Foreground Services for background tasks which need to be executed immediately, and WorkManager for deferable tasks. However, I do not need to update anything on the UI nor a notification (as they are mandatory in Foreground services) after the task is done...
What should I use?
UPDATE:
As said in the comment
OnDestroy is not even guarenteed to be called on process death and moreover. The correct approach is a foreground service that shows some generic "syncing with server" message until it's done
Related
I have a scenario where I need to notify the server whenever the device language changes. I need to call an API (even if the app is not running)
and update the current language with the server. I have implemented this with the help of a BroadCastReceiver for
<action android:name="android.intent.action.LOCALE_CHANGED" />
As soon as the broadcast triggered, I'm launching a service and calling the API. But, Since Android Oreo and above has background execution limits,
I have to launch a ForegroundService.
Here, during the background API call, a notification is visible in the Notification panel. So,
The user can still go to App settings and Force Stop the App, then the API call will be interrupted. But I need a guaranteed execution of this API. Is there any way to execute it in the background
other than foreground service? Can it be done with the help of WorkManager or Job Scheduler? What is the best way to handle this use case? Hope my question is clear. Thanks in advance!
The user can always kill your app so this should not be unexpected. But if you do not like foreground service, then queue this information (with timestamp) in DB and send to your API next time your app is running. If you must send immediately then do queue it too and then send immediately. If the user kills your app in mid-operation then you still will be able to resend it from queue next time. WorkManager is a good choice:
Guarantees task execution, even if the app or device restarts
My app is running an IntentService to download data in the background. While doing so, it sends Notifications to update the user about the progress.
The problem is, if the BatterySafe mode gets started, the IntentService gets killed, the Notifications stay there though (for example saying 30%).
Is there a way, to couple the lifecycle of the notification to the lifecycle of the service?
A second problem is, that I only want one instance of the IntentService. Is it possible, to achieve this?
In some Apps, for example the Avast AntiVirus, the background task seems connected to the notification, indicating that this background task is running. If this is a standard solution, it seems to solve my problem, so it would be great, if you could tell me the name.
I'm making an app in which i want a process always run in background e.g in facebook we got a notification and it will notify in our app. Kindly text.
Try Services and BroadcastReceiver to do this.
guess you need to explain the function you want in a detailed way.
Usually we will use a Service
or a Intent Service to do what you mentioned. If you want to detect a change in your application or the phone, you may register a broadcast receiver or a Content observer in the service depends on the function and effect you want.
But bare in mind that, service do not have UI so you should avoid to interact with users while using Services.
From my understanding, service can do most of the tasks you want. One example is play music. You can run a service in foreground if you want to ensure that the services is harder to be killed by the system when memory is low.
Intent service is used to handle asynchronous requests (expressed as Intents) on demand one followed by another. One good example is downloading a file
For Content observer, you will observe a content and the observer will react to if when there is any change from the "OnChange" method.
For broadcast receiver, usually we will use it to observe something happen, for example, screen unlocked, boot completed, sms received.
It really depends on your needs in order to decide what kind of services you want. Please explain in details in order to get more information.
There is some long processing that need to be completed, so I put it in a service. The activity must be able to connect to the service, show the user current results from the service. So I start the service with start Service and later call bind Service (with BIND_AUTO_CREATE) as in LocalService from http://developer.android.com/reference/android/app/Service.html#ServiceLifecycle. I want it to run until its job is done, and then self stop, even if client is still connected to it. (or determine the client to unbind) Is any way to do it with the sample LocalService?
I was considering passing a handler to the service so that it can send messages back to the activity, but I don't want the activity to get leaked. I am just getting used with services, so maybe I am misusing something.
EDIT: The workload consists of several threads, synchronized and run in parallel, so I guess is not a good candidate for intent service. Some data analysis is done in background service, and when the user restarts the activity that started the service, it should display some graphics according to current values computed by background service. All background processing is triggered at the beginning, and need only inspection later on, when activity connects to it. Android should not be able to stop the service. When the job is finished, the service should be able to terminate even if the activity is connected to it.
I just recorded a callback with the service. If the activity is not connected to service, it sets the callback to null. In this callback I call stopService() and then finish() on the activity. I am not sure that it is the best method, but it works fine for me.
If you want a service to be stopped when it is finished, I think what you are looking for is IntentService, they work as services, but run in another thread and when they are completed they dissappear.
Check this out
EDIT: NickT link is better, check that out! :)
I started learning android i've been playing with it and so far so good but i have some doubts about Services, i started learning them today so by gently if a say something very wrong.
For example, i want my app to grab some information over the internet from time to time, this polling period is defined by the user, then the UI gets updated. I though about creating a Service that run lets says every 30 minutes, gets the information and updates the UI.
If i get it right:
An IntentService just executes an operation and stops by itself sending the result through an intent(right?), so i think it's not what i want.
A Bounded Service is most likely used when you want IPC or allow binding from external apps, which again i think it's not what i want.
I think a Local Service is probably what i need, using a LocalBroadcastReceiver to update the UI, how can i make it to run the operation every X minutes( Handler postDelayed, ScheduledExecutorService or Alarm Manager ? )
If i understand it right a Service if not bounded can run infinitely if it's not killed due to low memory problems, making it a foreground Service is the safest ?
Last thing and it's kind of a noob doubt, if the user leaves the application(Click Home Button or opens other app) the app is still in background but the activities are in "Paused" or "Stopped" mode will the Service still be able to talk to them ?
Sorry for long post and thank you.
Your requirement : after every x minutes, start a service, pull some date, update UI.
Solution :
Define or set an alarm for every x minutes, to trigger a receiver.
From receiver start the service.
In the service, start an async task to fetch the data in doInBackGround().
Once data is fetched, from onPostExecute() send a broadcast to your activity.
In the activity have a dynamic receiver registered for broadcast sent from service.
From dynamic broadcast receiver update UI.
From what you've explained I wouldn't personally use a service.
The Android docs on services explain more but here is a snippet:
http://developer.android.com/guide/components/services.html
A Service is an application component that can perform long-running operations in the background and does not provide a user interface.
You could perhaps looks at using an AsyncTask, especially given that you only want it to run whilst the app is running:
http://developer.android.com/reference/android/os/AsyncTask.html
This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
There is a good answer here on how to run an AsyncTask repeatedly at specific time intervals: How to execute Async task repeatedly after fixed time intervals