I have read one post about Android services within threads but there is one thing that I did not understand. In the post the writer uses a custom service because it allows multitasking while IntentService does not.
https://guides.codepath.com/android/managing-threads-and-custom-services#custom-services
Until there everything is okay, but later the writer uses a HandlerThread which just allow one thread, as my point of view there is no difference between this and a normal IntentService.
https://guides.codepath.com/android/managing-threads-and-custom-services#threading-within-the-service
Am I right? or is there anything that I am missing? I am looking at this due I want to create a android service able to run different tasks at the same time, should I use ThreadPoolExecutor instead HandlerThread?
You should make yourself familiar with the changes to background execution introduced with Android 8.0 - you cannot freely execute background work in Services anymore the way you could when that tutorial was written.
https://developer.android.com/reference/android/support/v4/app/JobIntentService might be for you; if not, have a look at https://developer.android.com/topic/libraries/architecture/workmanager
Related
I am mostly confused between Services and Coroutines
I don't understand why should i be using coroutines vs when should i be using Services? When i am downloading a file, do i need to use service or can i just use coroutine? Both of them can not survive process death.
And if i can download a file using coroutines, what do i need the service for at all?
Can you please list all of these and give some examples where i would want to use Service, where would i use Coroutines and where would i use WorkManager?
Also, should i be using AsyncTask at all? Because i read that it has many flaws in it and it can create memory leaks.
I also read this article about background work:
https://developer.android.com/guide/background
but it talks about only Workmanager and Coroutines but no mention of services. Why?
Background Work
https://developer.android.com/guide/background
In Android and any UI application, you have the Main Thread. If you do something heavy on the Main Thread you will block the user's interaction. So you need to do such work in another Thread. You can do this on Android using:
You create a Thread
Executor with a pool of Threads
Handler
AsyncTask
RxJava
Coroutines
App lifecycle
https://developer.android.com/guide/components/activities/process-lifecycle
But the Android application lifecycle is a complicated thing. You can't know when an application will die. It is a process that consumes some amount of resources. When the OS has no resources it will kill some applications. So you have priorities and based on them - the application is less likely to be killed. And they are part of the above link. As you can guess - if the app is visible - it is highly unlikely to die. But what if you want to finish your work, but without keeping the user engaged. This is point 3 from the link:
You can use a Service
The idea of the Services is that you can do something "in the background", but it does not mean a background Thread. It means that it is in the background for the user. The plain Services starts on the MainThread. There are specific implementations that has their own Background Thread.
And also what a Service gives you - it will make your application less likely to be killed.
WorkManager
There are many APIs in Android. During the years they become more and more. But at the same time there are more and more restrictions - with the idea that the user's battery must be saved. Also the network resources. So it is hard to pick the right API, but at the same time workaround the restrictions.
So WM was invented. It is for cases where you want your work to finish for sure at some point. Even on a device restart. But you don't care that much about timing. Uploading a video is the best example. If you want something with an exact period like every 1 hour for sure - You need to use AlarmManager. With WM it might be 1 hour, 1 hour and a half, 4 hours, 6 hours, 1 hour periods. And for the video - it might start now, you upload 30MBs and in 2 hours - the other 70Mbs.
You should use a Service when you are actively tracking or engaging with the user. Like for example Music applications, Running applications, etc.
And WM is something that underneath is using a Service, Broadcast Receivers, Room, etc. It hides a lot of complexity for you.
In my app I import gpx and kml files to display tracks and waypoints on the map. For this operation, I use about 5 methods. Everything works fine, unless I try to import several long gpx/kml tracks with exfilechoser.
The screen goes black for some seconds, and logcat says: skipped frames ( > 600).
How could I get these calculations of the UI?
Or how could I use AsyncTask with about 5 methods? Is this possible?
There are several methods.
Do calculations asynchronously. Android SDK provides Handler, IntentSevice, AsyncTask, HandlerThread to solve problems asynchronously. By the way, it is recommanded to use multi-thread solving heavy calclations.
Let server do calculations. Mobile devices is not suitable for calculating, calculations consume power and affect app performance.
By looking at your question i will suggest you to use handler with java normal multi threading concept.
I dont think Asyntask is a very good approach to do long running tasks on background.
It's good have do calculation on server side it reduces the overhead on mobile site.
I think you should use Service which was designed for this purpose. It is said in documentation:
A Service is an application component that can perform long-running
operations in the background.
You should remember about creating new thread in service.
Caution: A service runs in the main thread of its hosting process—the
service does not create its own thread and does not run in a separate
process (unless you specify otherwise). This means that, if your
service is going to do any CPU intensive work or blocking operations
(such as MP3 playback or networking), you should create a new thread
within the service to do that work.
Hi i am implementing Sticker Feature in my app. Here user can download multiple sticker pack in background like Viber and Line.
I m a little bit confused of the what i use : Thread or IntentService.
also if user go to Sticker pack which is downloading or in queue then i need to show there.
After download i need to store it in database.
please help.
An IntentService has the advantage that the Service will be correctly started and stopped, when its daemon thread is busy. Communicating with an IntentService (using intents) is also simple and provides excellent isolation and thread safety.
If the IntentService is too restrictive (single threaded; one way, Parcelable communications), then you might want to roll your own bound ExecutorService. This is a fair amount of work: you need to manage starting and stopping the Service, handle binding and unbinding the service from client Activities, all the time being careful of thread safety.
... actually, I'm surprised there isn't a library, out there, to do this. It is such a common need.
Use Service with ThreadPoolExecuter instead of IntentService.
I'm very new to android technologies. I've recently read that android only allows a REST web service invocation from inside an AsyncTask... Is this true?? I'm developing an app for the university, I have to finish it for tomorrow and I would realy like to know if I can just call the REST WS inside an ordinary function, despite the fact that it may not be a good practice...
Thank you in advice!!
José.
The main thing is that you may not call it on the UI-thread (otherwise you will get an exception). Besides this restriction, it does not matter from where you call it.
A benefit of using AsyncTasks is that they are a very easy way of using additional threads in Android, since it comes with many callbacks (which are run on the UI-thread)
Another alternative could be to use an ExecutorService.
I am writing a home screen widget following Jeff Shakey's tutorial, http://android-developers.blogspot.com/2009/04/introducing-home-screen-widgets-and.html.
This tutorial uses service to avoid any ANR timeouts. I just wonder, can I use Thread instead of service to do the work of getting data and updating RemoteViews? I don't want to create a service, because Thread is easier to handle and pass data into Thread.
Thanks.
You should not rely on thread. It is not guaranteed to work. From experiences in a similar situation the thread approach worked on the emulator and the samsung galaxy but not reliable on the g1. So you should really attempt to use a service. It is quite easy: define in your manifest and startService. It is a little more cumbersome to pass data via an intent but it is manageable and the whole approach is definitely more robust.
I read an article saying that, to avoid ANR, service is sometimes not enough. Thread is needed.
See: http://blog.elsdoerfer.name/2009/06/03/writing-an-android-widget-what-the-docs-dont-tell-you/
"you are encouraged to use a service to perform your widget updates if you are doing anything that might take a little longer, in order to avoid Application Not Responding (ANR) timeouts. However, this will usually not be enough. ......
The solution is to have your service start a separate thread. For an example, see Jeffrey Sharkey's android-sky Widget."
Any thoughts?