I am writing a messenger app. When the app is started (and maybe in the future at boot time without UI), a background process should launch and receive the new messages. If a new message is received, it should show notification.
What should I use: AsyncTask or Service?
I think whatsapp uses Service.
AsyncTasks should be used for short lived tasks, like downloading file (small), processing data files, sending data to server. Services are for tasks which lifetime is a lot longer, as you say your app is all the time connected to server. So, you should use Service. You will have to maintain you own thread inside service for your networking tasks. You of course can create notifications from service.
Using service also causes that its less likely for your app to be killed by system if it is low on resources. Its good for such background app to keep notification with information that your app is working, also to make your app even less likely to be killed use foreground service (Service.startForeground() method).
Related
How to get data from the server when the Android app is closed when using the service and display in the notification
Example :
I'll design a program that communicates with the server every few seconds and displays the values received in the notification. Even when the program is closed or when the phone is turned on, it still has the information exchange server.
You can run a service using startForeground to prevent it from being killed.
However
A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory.
But still if you just want to send a notification you can integrate firebase in your server to show the notifications.
Notifications can even be shown when the app is not running in the background.
Setting up firebase in your android application:
https://firebase.google.com/docs/cloud-messaging/android/client
Since the inception of Android Oreo, services are killed as soon as Application gets in background state. You could use Foreground Service to avoid getting killed but that will display a persistent notification to the user.
Going by your needs, I would suggest you to use WorkerManager. It will choose best JobScheduler for you, and can perform task repeatedly as per your needs.
I am getting confused between service and Intend service,what is the difference between service and Intend service,then on which kinds of situations we have to use service,Intend service any can explain with some real time example?
Thanks in Advance
A Service is a piece of code that will run on your main UI thread and will remain running until stopped, even if you aren't in the foreground.
An IntentService is a special kind of Service that starts its own Thread and queues incoming start calls to run on that thread one at a time, in the order they came in.
Use Service if you need a place for long running actions to occur that need to continue even if the Activity is killed. For example, tracking location via GPS for a maps app. Use IntentService for repetative pieces of work. For example, downloading files. Or syncing a database. You can think of IntentService as kind of like an AsyncTask that runs in a Service.
I need to create a service that runs alongside the android app,irrespective of which screen of the app the user is on.
The app is a chat application so when the device is offline the service should queue up all the messages that are being sent offline and when the device is connected it should sync all messages.
I have written code for the job scheduler to sync data automatically when the device is online but while the app is active i would like to handle this manually.
Creating a Long Running service.
Operating system still can terminate the service in low memory and possibly other situations. There are 2 ways to overcome this:
If you are implementing the service, override onStartCommand() and return START_STICKY as the result. It will tell the system that even if it will want to kill your service due to low memory, it should re-create it as soon as memory will be back to normal.
AlarmManager .A system service, which will execute actions periodically. That will ensure that if your service will be terminated, or even the whole process will die(for example with force close) - it will be 100% restarted by AlarmManager.
Thank you.
You can do this by simple following steps:
Create Simple Service and after first launch of app just start at splash screen.
In Service after getting one data you can call another request.
After that you can create one broadcast action globally which will always call every network changed.
At background you can sync again data and saved it to shared preferences or as per your your requirement.
For interval you can also using AlarManager.
A part from this you can simply create Service using JobSheduler in this you can assign job and time as well.
Refer link :
https://developer.android.com/reference/android/app/job/JobScheduler.html
Hopefully this logic will helps you.
You have to use a intent service with sticky instead of service for this which will be executed in a queue and do your work. And since it is a intent service it will be started automatically after sometime, when system kills the service process.
I am parsing all text messages from the device and extracting words from them. For doing this I first used Service, but the issue with it was that it made application slower or sometimes I got notification that Application is taking longer to run.
Alternative to this I used IntentService. But problem with intent service is that whenever I stopped the application, I couldn't see my service running anymore. Alongside I also have to use Alarm Manager to schedule the things.
I am planning to use SyncAdapter for doing both of the things, but I don't think it would be a good option to use it. It would be really helpful if there is a better possible for doing this.
Background task might take upto 5-10 minutes for completion and I am planning to run it in every 12 hours. Though I won't be parsing old messages again. So it won't take longer after first time. The task should not end even when application is closed.
Basically IntentService is apt for background tasks which are not tied to the application lifecycle.
But problem with intent service is that whenever I stopped the
application, I couldn't see my service running anymore.
You can send updates to UI from intent service by using:
LocalBroadcastManager: how to use LocalBroadcastManager?
Handler: How to Collect info from IntentService and Update Android UI
Also you might want to see this video: The Zen of IntentService. (Android Performance Patterns)
EDIT:
Forget about using IntentService, it stops as the app stops because it runs on the same process as the app.
Since you want your service to work as a job every 12 hours, you could use a 'Scheduled Service'.
You can use JobScheduler or Firebase JobDispatcher API
Currently, I developing SNS application that can post comments on a article. and I want to process some network processes in background (e.g. Service, etc.). But I have some worry about that. Is it safe when user killing the application If I implement that features through IntentService?
I have planned all of 'Comment Request' stores into IntentService queue. but It takes a risk when user kill the application. (I guess Android OS have to destroys IntentService Queue)
So, I want to question about that. IntentService's intent queue is maintaining when user close (kill) the application? If not, how I handle this problem? using database or something to save IntentService queue? Absolutly, I have no idea with this.
If the user kills the application, services are safe. If they wish to go into their phones application manager and stop the service, that's entirely different, something that can't be stopped, so that's when the service would end. Your intent service will be fine. For more information: [https://developer.android.com/training/run-background-service/create-service.html]