I was working on a ble sensor client service which must communicate consistently with an activity and I was almost hopeless because of the android os restrictions and started service logic. You are solving a very complex issue about android services with a such simple example :) thanks
I love ur tutorial when u explain it very simple to understand for me. Keep it going mitch. I just stuck using bound service in mvvm and u the only reference that I had.
Please make a video to cover all points including started service, foreground service, Run service in background thread.....and android O..... And how to choose the correct one....
I do not understand this example, Mitch. I tried downloading the repo and run it. The app doesnt maintain state. There's no notification, like you'd see in a media app, and the app resets the timer if you swipe to close. What is the point of a service exactly? Because it doesn't seem to hold any persistent state
Cool stuff, thank you. You can get a reference to the service as soon as you bind to it, no need for the observer. Just do: MyService.LocalBinder binder = (MyService.LocalBinder) service; mService = binder.getService();
Hi Mitch Please answer this ,i want to make a call waiting app , and i want to post a notification when some one call the phone , I did it using Broad Cast Receiver but i want to do it when app is completely close . is this video what i need ?
your all videos are awesome. Can you make a complete video/split in series which has all the folder structure.. where to keep UI/ViewModel/Data/DB/Network with MVVM best practice.
Hey Mitch, love your tutorials! But if I may, isn't viewing a bound/started as service types (though not mutually exclusive) a bit inaccurate as a mental model? What exists is the Service class by itself - it can either be started, be started and bound to or be started as a consequence of being bound to. I think the folks have revised the documentation to reflect this representation as well.
I enjoy your tutorials and classes. Wondering if you could update with foreground services and bringing in more clean architecture like less knowledge in the activity about the service, etc. Thanks!
Hey Mitch, Great Stuff. Your content has helped me a lot in learning Android. I am starting to do videos on Android as well. I was checking your older videos, a huge improvement in video quality over time. What kind of screen recording software are you using? Thanks :)
I will start with general beginners Android topics. It seems these get the most views, to get the attraction. Eventually, stuff like building clone apps, or in-depth on a certain topic. You have some recommendation?
@@capitalcouch5095 if you are looking to maximize attention then definitely beginner stuff. The unfortunate thing about that is it gets very boring lol
Haha, well no pain no subs gain. Actually, I haven't seen many videos on beginners stuff but digging down in the implementation details inside android framework. What can we expect on this channel for the next year?
sending message to a Handler on a dead thread java.lang.IllegalStateException: Handler (android.os.Handler) {2f8aa543} sending message to a Handler on a dead thread not work
Great tutorial, I looking for solution of my problem and it's hard. I have an app which counting work time but when user closed app by accident I want that my time will be still counting cous of the service and whats happend that viewmodel updating view all the time in the same time ForegroundService do the same and my timer shows wrong value.
Great leacture about service. In a bound service, I want to implement the never-ending sevice after booting the smartphone. I know that case in a started service but I don't have any clues in the bound one. Could you give me some comments?
I have a guestion.. Let say i want my app to run a service that uses Sensor and calculates Steps and than return each step to Activity. A. Should it be On another Thread so it wont freeze main thread? B. Should it be Bound ?
******************** WARNING: BUG ************************ Thanks to @Pemba Tamang for finding this bug. Inside the "run" method of the runnable inside MainActivity, change the "aBoolean" variable to "mViewModel.getIsProgressUpdating().getValue()". If you don't do this then the runnable will continue running forever and spawning new instances. It's due to the "aBoolean" variable being final and then being used to control whether the runnable runs or not. If it's final, it will never change. So obviously the runnable will continue running forever. Here is a gist with the updated run method: gist.github.com/mitchtabian/0621da267a677cabcd1f270e3c6513fa
Thank you for your great video. I have a question that is it possible to given a service bound by multi-activities? so that no matter which activity is running, the service will keep running without re-create?
@CodingWithMitch Atfrist, thank you for offering so much of your time to help noobs like me. Your Video is pretty good, but as a experienced Beginner, there are some questions left open. @25.40 As I understood the point of MVVM is to encapsulate the way of getting Data and the displaying of Data, to create a better structured code. So why do we bind the Service to the Activity and pass the ViewModel class instead of starting the Service in the Activity, but bind the service directly to the VM.class? As I understand, in your video, we create a bridge from Service to Activity, which continuous to the VM and the data later on will be send from the VM to the Activity. Can you tell me where I went wrong?
This is actually an anti-pattern btw. You shouldn't bind services like this. Check out this article medium.com/androiddevelopers/viewmodels-and-livedata-patterns-antipatterns-21efaef74a54
Awesome tutorials.. but It is only for simple Services. What when I have a location service which returns ? I can do all that stuff like "calculate distance" "find speed" "change to LatLng" in service and do some getters methods but I thing it isn't right according to mvvm pattern. So I need to observe data in Repository somehow? There is a simple solution for this? Obviously I can get location from bounded service to activity, like you did, then viewmodel.setvalue(location). From viewmodel send to Repository, do the staff, and observe it, but it isn't right, isn't it?
Thanks a lot. What is the difference between Services and Android Job Scheduler? and when to use each? In one of my projects I used Android Job Scheduler to Schedule Push notifications twice a day. Thanks
This is awesome.Just had one doubt. Is it not wrong to implement the "Service Connection" inside ViewModel as it is against the concept of MVVM as ViewModel should not contain anything related to Android Framework or android framework apis.
That is the ideal case, but I think it's more of a KMM perspective, i.e. if you're targeting multiple platforms like iOS and Android using KMM, it makes sense to encapsulate all business logic in the view-model minus the Android Framework-specific stuff.
This was the best tutorial I found on the internet about Bound Services, but the fact that it uses MVVM is driving me crazy, because I can not rewrite my whole project to implement MVVM. Does anyone have a version of this code that does not use MVVM? I watched the video about MVVM to try to "remove" it from the code, but unfortunately I could only get my code to stop working.
No problem just write service binder instance or create service connection in directly activity meaning remove viewmodel and write service code in mai activity which is in viewmodel
@@codingwithmitch I don't understand.... the river of true false true false kept going on in my log...is that how it works?? isn't it bad for performance.?? please explain a bit more
@@pembatamang8233 It's the runnable. It's constantly asking the service for updates. Every 100 ms. So if you put a log there, you'll see it run constantly.
@@pembatamang8233 Whoops. There was a bug. Good find. Inside the "run" method you can't use "aBoolean" because then it's final. Which defeats the purpose because it will never change. Use "mViewModel.getIsProgressUpdating().getValue()" instead of "aBoolean" inside the run method. gist.github.com/mitchtabian/0621da267a677cabcd1f270e3c6513fa
If you could have just explained in a simple video, what bound service is and how to implemented it that would have been much nice, using a simple example this video could have been much better and also lesser in time and code. Just like coding in flow. But still thanks :)
Very nice tutorial. But as far as I understand, two separate runnable run at the same time in the activity and in the service to show the progress. It could be better, one runnable runs at the service and inform the progress to the activity via broadcast-broastreceiver or LiveData implementation in connection with viewmodel.
I was working on a ble sensor client service which must communicate consistently with an activity and I was almost hopeless because of the android os restrictions and started service logic. You are solving a very complex issue about android services with a such simple example :) thanks
Background thread service tutorial please :)
Instablaster...
I love ur tutorial when u explain it very simple to understand for me. Keep it going mitch. I just stuck using bound service in mvvm and u the only reference that I had.
Damnn Mitch you are the best. Precise and to the point. Very well explained videos always.
The best tutorial on services I ever seen
This video about service is best video on services.
This video cleared my all doubt 💯💯.
Thanks a lot Mitch
Thank you so much Mitch, that very cool explanation. I request you to please create more content on Services and running task in BG
Love the way you made everything so simple
Really Really Really so nice and clean directions about bind services
😘😘😘😘
Thanks Mitch for an awesome tutorial ☺️
How can you run a service in a background thread? Please please make a video on it. Thanks!
Fabulous explanation, Thank you so much, Appreciate your efforts.
this is exactly What i neeed!! Thank you from South korea
That will be great if you can make video on startForegroundServices which is must for Android O devices.
Please make a tutorial of forgroundService for android O.
Good work, thanx for the lecture man. Helped a lot
Please make a video to cover all points including started service, foreground service, Run service in background thread.....and android O..... And how to choose the correct one....
Yes we need video on background thread
noted
@@codingwithmitch link?
my mind blown :D i believe we need to go deeper and do more exercises with ur help :D
I would be great if you have added an example of aidl in this video
Some engagement getting you to 100k
Make video on Starting service in ANDROID.O and changes in O. Your explanation is amazing with architectural design.
awesome video. learned a lot .
I do not understand this example, Mitch. I tried downloading the repo and run it. The app doesnt maintain state. There's no notification, like you'd see in a media app, and the app resets the timer if you swipe to close. What is the point of a service exactly? Because it doesn't seem to hold any persistent state
That's so fantastic and useful.
I appreciate the love brother :D
did you make a video to run service on bg thread?
Cool stuff, thank you.
You can get a reference to the service as soon as you bind to it, no need for the observer. Just do:
MyService.LocalBinder binder = (MyService.LocalBinder) service;
mService = binder.getService();
the MVVM pattern sure is a good pattern, but i wouldnt recommend using it in a guide video since it can confuse newer programmers.
Wouldn't new handler and runnable be created every time the progressUpdating get onChanged this way?
what is your favorite architectural design pattern xD?
thank you Mitch... another great video! made ma day ?
Really thanks for this lecture, sir. could I ask you, when to use Service, when to use IntentService, when to use JobIntentService?
hey i am intested in the to run the service on background thread.
Thanks for the video, it was really helpfull
Dude you are awesome...
please make a video on background thread
Please make a video on what can we do on background service thread.
Hi Mitch Please answer this ,i want to make a call waiting app , and i want to post a notification when some one call the phone , I did it using Broad Cast Receiver but i want to do it when app is completely close . is this video what i need ?
your all videos are awesome. Can you make a complete video/split in series which has all the folder structure.. where to keep UI/ViewModel/Data/DB/Network with MVVM best practice.
Yes please do a video on running services in a background thread
noted
Best tutorial ever!
Hey Mitch, love your tutorials!
But if I may, isn't viewing a bound/started as service types (though not mutually exclusive) a bit inaccurate as a mental model? What exists is the Service class by itself - it can either be started, be started and bound to or be started as a consequence of being bound to. I think the folks have revised the documentation to reflect this representation as well.
I would love to see a service running in the background .... please make a video on it
I read ViewModel should not contain services, because Service has it's own lifecycle. Service field will lead to context leak.
I enjoy your tutorials and classes. Wondering if you could update with foreground services and bringing in more clean architecture like less knowledge in the activity about the service, etc. Thanks!
How does the whatsapp show there call ui when someone calls
what was the real use case of services like that? in your experience. looks quite difficult to understand if learning it on your own...
Great video!
And could you please make some videos about the whole services thing because it's confusing
Hey Mitch,
Great Stuff. Your content has helped me a lot in learning Android.
I am starting to do videos on Android as well. I was checking your older videos, a huge improvement in video quality over time.
What kind of screen recording software are you using?
Thanks :)
Hey, it's camtasia.
What kind of videos will you be making? Like what topics?
I will start with general beginners Android topics. It seems these get the most views, to get the attraction.
Eventually, stuff like building clone apps, or in-depth on a certain topic.
You have some recommendation?
@@capitalcouch5095 if you are looking to maximize attention then definitely beginner stuff. The unfortunate thing about that is it gets very boring lol
Haha, well no pain no subs gain. Actually, I haven't seen many videos on beginners stuff but digging down in the implementation details inside android framework. What can we expect on this channel for the next year?
sending message to a Handler on a dead thread
java.lang.IllegalStateException: Handler (android.os.Handler) {2f8aa543} sending message to a Handler on a dead thread not work
Hi Mitch, I am new in android development, where do you think is better to access a service, from view or ViewModel? Thanks
Please make a tutorial of forgroundService for android O and also on WorkManager and AlaramManager with broadcast
Great tutorial, I looking for solution of my problem and it's hard. I have an app which counting work time but when user closed app by accident I want that my time will be still counting cous of the service and whats happend that viewmodel updating view all the time in the same time ForegroundService do the same and my timer shows wrong value.
Great leacture about service. In a bound service, I want to implement the never-ending sevice after booting the smartphone. I know that case in a started service but I don't have any clues in the bound one. Could you give me some comments?
I have a guestion..
Let say i want my app to run a service that uses Sensor and calculates Steps and than return each step to Activity.
A. Should it be On another Thread so it wont freeze main thread?
B. Should it be Bound ?
perfect , thank you
******************** WARNING: BUG ************************
Thanks to @Pemba Tamang for finding this bug.
Inside the "run" method of the runnable inside MainActivity, change the "aBoolean" variable to "mViewModel.getIsProgressUpdating().getValue()".
If you don't do this then the runnable will continue running forever and spawning new instances. It's due to the "aBoolean" variable being final and then being used to control whether the runnable runs or not. If it's final, it will never change. So obviously the runnable will continue running forever.
Here is a gist with the updated run method: gist.github.com/mitchtabian/0621da267a677cabcd1f270e3c6513fa
its Tamang
Thank you for your great video. I have a question that is it possible to given a service bound by multi-activities? so that no matter which activity is running, the service will keep running without re-create?
Sir please make a video of integrating exoplayer using services and getting realtime feed from the service of running music.
Please make a background thread video :)
I would like to have seen the service callback with progress reports rather than MainActivity polling for progress.
how to run activity in the background in the android
@CodingWithMitch
Atfrist, thank you for offering so much of your time to help noobs like me. Your Video is pretty good, but as a experienced Beginner, there are some questions left open.
@25.40 As I understood the point of MVVM is to encapsulate the way of getting Data and the displaying of Data, to create a better structured code.
So why do we bind the Service to the Activity and pass the ViewModel class instead of starting the Service in the Activity, but bind the service directly to the VM.class?
As I understand, in your video, we create a bridge from Service to Activity, which continuous to the VM and the data later on will be send from the VM to the Activity.
Can you tell me where I went wrong?
This is actually an anti-pattern btw. You shouldn't bind services like this. Check out this article medium.com/androiddevelopers/viewmodels-and-livedata-patterns-antipatterns-21efaef74a54
Nice work and please create a video for android oreo background services and background location
Awesome tutorials.. but It is only for simple Services.
What when I have a location service which returns ? I can do all that stuff like "calculate distance" "find speed" "change to LatLng" in service and do some getters methods but I thing it isn't right according to mvvm pattern. So I need to observe data in Repository somehow? There is a simple solution for this?
Obviously I can get location from bounded service to activity, like you did, then viewmodel.setvalue(location). From viewmodel send to Repository, do the staff, and observe it, but it isn't right, isn't it?
Thanks a lot.
What is the difference between Services and Android Job Scheduler? and when to use each? In one of my projects I used Android Job Scheduler to Schedule Push notifications twice a day. Thanks
Too much to say about that. You should just read internet articles. I don't have a post on the differences.
This is awesome.Just had one doubt. Is it not wrong to implement the "Service Connection" inside ViewModel as it is against the concept of MVVM as ViewModel should not contain anything related to Android Framework or android framework apis.
That is the ideal case, but I think it's more of a KMM perspective, i.e. if you're targeting multiple platforms like iOS and Android using KMM, it makes sense to encapsulate all business logic in the view-model minus the Android Framework-specific stuff.
This was the best tutorial I found on the internet about Bound Services, but the fact that it uses MVVM is driving me crazy, because I can not rewrite my whole project to implement MVVM.
Does anyone have a version of this code that does not use MVVM? I watched the video about MVVM to try to "remove" it from the code, but unfortunately I could only get my code to stop working.
No problem just write service binder instance or create service connection in directly activity meaning remove viewmodel and write service code in mai activity which is in viewmodel
can you please make a video on media code API of android
I put a log under aBoolean in the MainActivity and after the work is over, the aBoolean keeps changing. Why is it happening??
It's supposed to. That means the work is done
@@codingwithmitch I don't understand.... the river of true false true false kept going on in my log...is that how it works?? isn't it bad for performance.?? please explain a bit more
@@pembatamang8233 It's the runnable. It's constantly asking the service for updates. Every 100 ms. So if you put a log there, you'll see it run constantly.
@@codingwithmitchok... how do i stop it when the work is done??
@@pembatamang8233 Whoops. There was a bug. Good find. Inside the "run" method you can't use "aBoolean" because then it's final. Which defeats the purpose because it will never change. Use "mViewModel.getIsProgressUpdating().getValue()" instead of "aBoolean" inside the run method.
gist.github.com/mitchtabian/0621da267a677cabcd1f270e3c6513fa
Great sir.. please share on intent service
please make a video on threads with practical example not just print log messages
If you could have just explained in a simple video, what bound service is and how to implemented it that would have been much nice, using a simple example this video could have been much better and also lesser in time and code. Just like coding in flow.
But still thanks :)
Haven't watched it yet but Is this video part of that Spotify alike app?
Nope just bound services
pls use microphones for clear voice.
thanks
It is a great tutorial. Can you please make a tutorial for Services for Android O or higher
nice please make more video on service
Awesome
Great tutor
Please do a video on background thread.
checkout his course on Threads, It's on his website and on pluralsight also... It's pretty cool
thanks I noted your response
thx 👍👍🙂
Make a video for infinite running servicee PLEASE
awesome tutorial.. can you please give a clue how to integrate this on playing an audio.. thanks
Boom codingwithmitch.com/courses/android-audio-streaming/
big Mitch
Very nice tutorial. But as far as I understand, two separate runnable run at the same time in the activity and in the service to show the progress. It could be better, one runnable runs at the service and inform the progress to the activity via broadcast-broastreceiver or LiveData implementation in connection with viewmodel.
please do video
Better: Use AndroidX WorkManager from the new Android Jetpack Library
Please make a video on what can we do on background service thread.
Background thread service tutorial please :)