My question is can I close the activity after starting work manager?. currently in my app, I want to use WorkManager to print a receipt from the printer. So I want to start WorkManager from the activity, then I need to close the activity. Will the code from the WorkManager continue the print task from the worker or will it stop after closing the activity ?
Short Answer :- Yes
that's the whole point of the using the workmanager so you can schedule your task based on System events, so it does not not matter what activity is running.
as according to the documentation
For work that is deferrable and expected to run even if your device or
application restarts, use WorkManager.
https://developer.android.com/guide/background/#workmanager
About how to implement workmanager, below documentation will help you.
https://developer.android.com/topic/libraries/architecture/workmanager/basics
here at the end you will find section "Hand off your task to the system".
the system will place your task in a system queue to execute it based on system event that you will define
Related
I have an application with background service (important tasks run in background always).
What is your offer for handle important tasks?
my application may be close but these tasks must be done.
If your service is doing important task and the application mustn't be close you should use Foreground Service as it will inform the user that you're doing some work.
If you don't need immediate execution you can use WorkManager. WorkManager offers deferrable but guaranteed execution, it'll run even if you're application is killed.
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
I am designing an app that has weather info to display at home screen. I want to update weather every 10 minutes.
I have a issue to update UI when home activity is in background (another activity is open upon home activity)
some help but issue in updating UI:
Scheduling recurring task in Android
Please advice me if another way to do this. Thanks in advance.
Use AlarmManager:
Periodically executing background tasks
Executes even if application is not running
Used when long running task is not required forever
For most cases, setup a Scheduler that triggers a background service at regular intervals
Suggestion: Use IntentService in conjunction with the AlarmManager
Reference & link: AlarmManager - CodePath
I have gone through your question and I am glad to give you the appropriate answer based on my personal experience.
Well if you are interested in gathering the weather updates every 10 minutes even when the application is in background, then I suggest you to use Background Service.
You can't imagine the advantages of background service. There are three kinds of background service
Started Service
Intent Service
Bound Service
All of the above three has there own usages. But in your case i suggest you to use the Started service. This service will start when you will call the startService() method, and will stop when you will call the stopService() method. Using the background service over Alarm Manager is recommended, and implementation is also quite simple and you can go through this link to understand the background service.This service will also keep on running in background even when your app is in background and will also gather the weather data every 10 minutes.
According to https://github.com/ReactiveX/RxAndroid/issues/257#issuecomment-164263215 . interval is just for active code, and if app is not wake up, it will not work. So how to use interval for background scheduling tasks?
Please DO NOT use this solution:
To use interval from RxJava you'll have to make sure your app's process stays alive. One way to do it is to put use the Observable in a foreground service. This is a bad idea because the service is NOT actively delivering value to the user. Waiting for time to pass is not delivering value for the user. Again please DO NOT use this.
AlarmManager and JobScheduler (or it's backport GcmNetworkManager) are far better choices for repeating background activities. If you use AlarmManager.setInexactRepeating() the system can batch jobs from multiple apps together to save battery. Using JobScheduler enables you to execute your background jobs in specific conditions, eg. when the device is connected to the internet or when battery is more than 20%. (Internet is required to check the weather).
interval from RxJava does have it's usage on Android. It's an excellent replacement for Runnable.postDelayed for relatively short durations. It makes the code shorter and more readable.
If you need to schedule a task that should be run even if app is not active anymore then use AlarmManager.
If you need to schedule a task that should be run only when app is active then you can use Observable.interval() and react on emission to execute some code and please don't forget to unsubscribe from the Observable when appropriate (when Activity is paused, etc) so app won't burn the battery!
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