The Ultimate Guide to WorkManager (with Jetpack Compose) - Android Studio Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to a new ultimate guide in this case it's about work manager which i realized i don't have a video about yet on my channel so here it is it's a full guide about work manager that really teaches you everything from a to z from the ground up you have to know nothing about it you can just watch this video and at the end you will have a really good understanding and a really good impression of what work manager is how it can be used and how especially you can use it in your project and for what kind of use cases it actually makes sense so what is work manager actually in android and when do we use it work manager is yeah it is contained in the jetpack framework i think yeah it is a jetpack library and it is it is used if your app needs to do something that needs to be executed really reliably so even if your app is closed or even if the device is restarted if you schedule a specific task your app should perform then work manager is the the way to do this so it will make sure that it will be executed even if the device is restarted even if your app is closed even if your app was killed or so so if you have something to do that's that needs to be done really reliably then pick work manager in general there are three different types of tasks you can have with work manager that where it makes sense on the one hand those are immediate tests so just tasks that need to be done immediately that will finish soon then you have a long running tasks which well run long for example a large file download or so and then you have deferable tasks so those are tasks that you don't want to do immediately instead that you want to kind of schedule at a later point and then what work manager does it actually helps you to schedule these tasks so it will either take a one-time task so something you only want to do once or you can also use it to schedule periodic tasks so tasks that you want to do over and over again given a specific interval for example if your app needs to synchronize with your server every one hour or so then you can use workmanager workmanager to do that so your app does not need to be launched but workmanager will make sure that your tasks that your synchronization process will be executed once an hour and internally work manager will just take your task and store it in a database you don't see any of that that's just how it works internally so that it actually does not forget about your tasks even if the device is rebooted so all these tasks are just stored in a database and even though i could now just go through the android documentation and go through all the work manager topics step by step i don't think that's really helpful to learn this that's why i actually thought of a little bit more practical example so that will really help you to understand how work manager how that works and how when you should actually use it how you can use it how you can actually yeah basically take all its potential and work with that let's actually take a look here in my on my phone where i will show you the app that we will build right now you only see a simple start download button because that's what will happen here when we click start download we will download a quite large picture from my website basically my profile picture from youtube and then we will take this picture that we downloaded we saved in internal storage and after that we start another worker so another work manager task that will put a color filter on that picture so this image processing uh tasks are usually yeah quite cpu heavy and something that makes sense to be done in work manager here we will add quite some artificial delay so we can just see how that really works usually that would be much quicker than it is here but yeah you'll just see some delay let's just click start download and see what happens you can see downloading color filters blocked just gives us some information about the current workers about the current task once it's downloaded it will show up here and you can see now it's applying the filter and after five seconds it will update the image with the green color filter here which actually is done by two different work manager tasks so you will learn how you can take these and also chain these together so that you have one work manager task that takes some input has some output then another work manager task receives that input from task number one gets it gives us some output and we can then make use of that no matter if our app is actually launched or not but that's not it yet what you will learn you will also learn about work constraints which means you can kind of set constraints that need to be fulfilled for your work to start so here for the download it of course only makes sense if your device is actually connected to the internet so we can actually specify hey work manager please only execute this task once there is internet available and if you schedule it for like in in 30 seconds but the user does not have internet connection enabled or just isn't just connected then work manager will keep that task and execute it as soon as there is internet so you can try this out here i can't show you this because once i turn on internet turn off internet here then it will simply close the connection since i'm connected via wi-fi it will work i tried it so it will simply wait with that download until you then turn off airplane mode again or so just regain internet connection and yeah that's something that's really cool with work manager not just about internet but i will go through that in a lot more detail once we get to these work constraints and you will also learn how you can simply show a cool download notification once your worker is active not sure if it shows up again um for some reason sometimes it does not show up maybe the task is not long enough uh let's wait until this finishes here and then we can try once more yeah now you can see download in progress um you're not not sure why it doesn't always show up but yeah with workmanager you can also easily show such notifications just like in a foreground service i would say without further ado we just jump right into it and start from absolute scratch here all you need to make sure is to get the initial dependencies down below from github as usual you can see two compose dependencies for a view model and a live data now that's just to convert live data to compose date which we need to do here we have coil to show an image yuri in an image composable which we need to do here since we save these images on our internal storage we have curtains um actually that's not cod numbers plus curtains that's a work manager dependency let's swap that out work manager with cool routines so there are different ways you can use workmanager with we will use curtains of course because it's cool and we will use retrofit to make to actually download the image from my website to save it in internal storage so far so good main activity is completely empty and our whole project is completely empty so i would say we just jump right into it and i will explain everything from scratch so yeah enjoy the first thing we want to do is we want to set up our api our retrofit interface which just contains a single function to simply download the image to our internal storage so here in our root package i'll just create another carton file i'll call it file api select interface that will be a get request and if you want to use the same image as i do then simply paste this stuff in there if you are too lazy to write this off simply paste it from the github link down below and that will just be a suspend function get rather download image and that will return a response of type response body so that's just how we download the file with a retrofit that's pretty easy so in the end we just get a byte stream um or by array and we can simply then yeah kind of take that stream and write it to our own memory we want to also have this one here as a singleton so i won't use any dagger here for dependent injection because i really want to focus on work manager so what i will do is we will just have a variable here instance and we say bye lazy and here we initialize this retrofit that builder we say base url is in my case simply my website and then we can call build and dot create passing file api double column class of java so we really don't need any decent stuff here we're not dealing with a json responses we just need this base url here and then we're ready to use this api cool should work with any other image as well just that you have something if you don't know what else you should use you can also use this one the next thing is we will create a so-called worker what is a worker a worker is basically a class that defines our work manager task so let's just create this and we will see how this looks like this will be the download worker because that's what it the class really does that's what the task is about it should download our image and this should potentially also be something very large so to download an image you not always use work monitor but you could imagine that's like a one gigabyte file and then that's a long running task that shouldn't be interrupted and that should also run reliably even if your app closes we make sure that inherits from curtin worker because we want to use work manager together with coroutines and we can dismiss this message here we need two parameters on the one hand we need the context on the other hand we need so-called worker parameters and these will contain more information about the specific worker we will get to this don't worry passing the contacts in our params here worker params and now this download worker needs to implement one function we can press ctrl i or just implement do work here and that's the function that performs the actual work you can see it's a suspending function we can directly make our network calls in here and we then return such a result which comes from the work manager api and the workbench api will then use that result to determine okay was that task actually successful was that download successful did something fail maybe we got an invalid response or um should this actually be retried later because that's also possible with the work manager for example here when we download something and there is a server side error so we get an http status code starting with five indicating that there was an error that was the server's fault then usually trying that again later fixes this issue so in that case we could say okay work manager please um retry this request okay so in this function we now need to download our image using our api first of all before we do this i want to show you how we can also show a little notification with this because work manager already contains this behavior to easily show notification since in that case if you have a long running test it will just execute that in a foreground service in the end and as you maybe know a foreground service is just a service the user is actively aware of so the user knows okay this service is running because they will see a continuous notification and work manager will handle all that foreground service stuff for us but we just need to give it some information about the notification we want to show of course and when we want to show a notification then we need to create a notification channel that's usually done in an application class so let's create that here called download application and here from application and here when we say on create we can say we can just create our notification channel here let's actually take a look how that looks like we only want to do this from android oreo onwards so we've built version import build sdk end if that's greater than equal to build version codes oreo and we want to do this because because before that version we don't need to create a channel so we will define a channel here his notification channel if you give it an id um we can use name parameters here let's just hard code this to download channel and then a name would be file download maybe or image download and for the importance what do we pick here we pick importance hi notification manager important hi nothing really special here um if that's new to you don't worry about it too much we just give we just specify some information about our about the notifications we send into this channel and here we next actually want to get the notification manager instance using our context which we already have since we're in the application class so we can just say get system service um context notification service and i think yeah that's already it we can also just say notification manager double colon class java that should also work and then we use this to simply create the notification channel passing our channel instance here and then we're good to go we need to make sure that we take this application class and put it in our manifest here application name download application and then we can jump back to a download worker in which i want to now create a function that shows this notification that will be a privacy pen function show notification and here since we are inside of this curtin worker we can simply say set or was it called set foreground new it is set foreground so we have set foreground async that does not need to be run in a curtain or we have set foreground that is suspending which is fine to be used here that takes a foreground info which is simply an object we can create here and we need to provide another notification id for which you can just randomly generate one doesn't matter and a notification so how our notification looks like we can use notification compat dot builder we need to provide the context here and the channel id make sure you choose the same channel name as you specified in your download application file usually i would create a content for that but i'm too lazy here for this project isn't it that isn't a real project when i set a small icon um we need to provide and i can resource here so let's do that let's import an icon um or maybe we already have one let's check out a drawable um let's just choose the launcher icon because why not and then we can say set content text that's the description here we can say okay downloading we want to set a title um download in progress and then we can say that build and there we go we now just have a show notification function maybe we should rename this to start foreground service because that's what it actually does and then here and do work we want to just immediately call this here and then i want to simulate some delay of five seconds as i said we will just use this here so you can just better observe what happens when we click start download because without this everything would be happening very quickly and for educational purposes i think it's it makes sense to add some delay here we'll also do this for the color filter worker which we'll implement afterwards that will put the green color filter on top of our image and now let's see how it works to actually download a file using retrofit we simply get the response using our file api instance download image so we use our singleton and then we check if the response body is actually not equal to not so the body contains the the byte information about our image now that we downloaded you can see that suspending so we will only resume here if we actually downloaded it or if if something failed in this case so that means the download was successful if the body is not equal to now then we just want to return with context blog using our io dispatcher so we just make sure that this cartoon is made safe since what we now do is we read these buys and save them on our internal storage and that should be done on the hours io dispatcher so let's do that here we first of all want to create a file object here to specify where and how we want to save this specifying a parent so just the path basically for which you can use our cache directory um usually that's probably rather something for like temp files or wherever you want to save that like external storage could also be probably more thing um however that requires some more setup you need permission scope storage so i really wanted to leave that out and i will pick the simple option here of simply saving it in our cache directory which belongs to internal storage and the child is simply the file name which i will pick image.jpg for it's that easy then we can construct an output stream to simply save that file using that output stream that's a file output stream and we pass our file in that and we see we get a little warning which we can get rid of by adding this annotation here suppress this for the class it's it basically thinks we're doing this on the main thread even though we aren't since we are inside of a suspend function but yeah it doesn't know that because it's an old java api or the old java warning and then we can say output stream that use here we get an instance to this stream which we can now actually use to write something into this i'll use a try and catch block here since we're going to catch i o exceptions in case something goes wrong in the trial block we want to say stream dot right and well that now takes a byte array of bytes that we want to write into the stream so into this file and this byte array comes from our response buddy from retrofit so we can just say body that bites and here if we got an exception so if something went wrong we want to return with context now we need to return a result so when i say result from work manager here listenableworker dot failure in this case because something went wrong you can see we can either just normally return a failure or with an output data object do we want to attach some output data here yeah we do so in this case we could simply pass an error message of what went wrong so we actually know that and we can simply do this by constructing a work data off object and that's pretty similar like a normal map so just a list of pairs you can say and we can simply construct these using a key and a value so we can say okay this type of key contains that value which is the data we want to then later access in our ui and for that i will create a little um constants file so here worker params which just stores the single keys we have here and that's on the one hand the error message that's the image yuri that we will later simply save here to know where we actually find the saved image and that is the filter yuri so that will be the imagery of the successfully uh filtered image so the image with the color filter so we will just save two images here so we just keep them both just for reference you can also override the other one then you don't need the second key over here you want to map the worker params let's better call it worker keys i think so shift f6 work a keys dot error message to e.localize message because that's what we want to return here cool then what's next we now still get an error because that does not return a result here like if if everything went well it does not return a result so down here if we reach that point and we haven't returned here we know everything was successful so we can say result success and boom the error is actually gone however we also want to attach some data of course in the success case which is our imagery where we want to where we save that image in the end so same game as before work data off and this time we say worker keys imagery and we map this to our file uri so file.2uri sure you use this theory not this one and two string because we can't put yuris in this work data object we need to use primitive data types and strings and stuff like that and we can then later just reconstruct the theory of that string cool so now we consider the case that we got a response body what happens if we did not get a response body then that means we would actually go on down here and then we need to check what went wrong so if the response is actually not successful here we're gonna find out what was actually not successful so why it wasn't success uh yeah why it wasn't successful so we can check if the response code like the http status code um two string starts with a five that means we got a server-side error as i mentioned already in that case we would like to retry our request at a later point so we can simply um return result dot retry it's really that easy however if the response code starts with a 4 or a 3 which is also an invalid or an error response then that means that was an issue of the client usually so in that case we don't want to retry it so we can say return result failure passing our work data off again this time again with worker keys error message to invalid server response or so or we can say i don't know network error and after that if statement um if it was successful and reached that point which should never happen we can return result of failure with maybe um simply an unknown error message unknown error cool and that's our worker that's our worker that will now work independently of our app so even if we start this worker and then we close our app it will go on if we schedule this worker for example in 10 minutes and then we relaunch our device then it will still fire off so it will still be scheduled and be executed then in 10 minutes or when the device is launched the next time so that's pretty cool however we want to chain this now with a different worker and that will be the worker that will put the color filter on top of this image to save it again and yes we could theoretically also just do this here this will probably save some work but i want to show you how we can actually chain this and how another worker can receive the inputs from broker one basically and how we can then finally get the result in our ui so let's go here root package and create our color filter worker select class same parameters as before like this and make that inherit from creatine worker passing these two values and implementing do work cool so far so good that is already nothing new however this worker now actually takes some kind of input data so since we launched this of course and after our download worker it needs to have the uri as an input type where it can actually find the image it should put the follower the the color filter on and how do we get this well well um image theory and we can now get this using these roka params that each worker needs to implement you can see we have a bunch of uh fields here and one of these is input data so we can use that and that just gives us these key value pairs we pass from our other worker so we want to get the string worker keys imagery of course and then we want to transform this to a yuri since that's now a string that we got and that to a file so we have the file again and let's actually rename this then to just image file or so and that's how we now have the file in this worker we can now use to create a bitmap out of that and uh yeah simply put a color filter on that and again i want to put a little five second delay here so you can see that better later and then we can simply return image file outlet so we just make sure that's not equal to now and in that case we now want to get that file as an editable bitmap because that's how we can simply put a color filter on that well bitmap is equal to bitmap factory decode file and we can simply pass file file dot absolute path and that will give us the contents of that file in form of a bitmap then we want to have a result bitmap so the new image with the attached color filter which is equal to bitmap copy so we create a copy we use the same config as we have for bitmap and for immutable we say true so we can change it then to use the color filter i will use a simple canvas so val um paint is what we need for that so we simply create a new paint object yeah that's fine and we want to say paint.color filter is a new lighting color filter for which we need to specify these mole and add values i have no idea what that is exactly for model is definitely the color that we want to attach but yeah just specify what i spend over here and it will look green so um just a color in hexadecimal format which is 0 8 ff04 that's my green type you can choose any color you like and the add value will be one then we can construct a canvas out of that um bitmap android canvas make sure you don't import the compose one if you want to learn everything about canvas check my courses down below i have a very very detailed course about that um the result bitmap here for the canvas and then we can say canvas dot draw bitmap passing the result bitmap we're going to start from the very top so zero f for left top and we pass our paint object which contains the color filter cool and now we simply need to do the same thing again to specify an output stream to save the file in internal storage so we can simply say with contacts dispatchers io same as before and here we will first of all specify a file well like result image file is equal to a file from java i o path name is again context cache directory i'll call it like maybe yeah let's call it new image.jpg we can then say okay um we actually have an output stream same as before file output stream passing the result image file again getting rid of this warning and then we can see okay was saving that actually successful for which we can use results bitmap.compress and that's how we save a bitmap using an output stream the format is a bitmap compress format.jpg of course the quality is let's pick a 90 percent and the stream is our output stream and it returns a boolean whether that saving was successful or not so after that we want to return if successful result success with output data this time we choose our worker keys dot filter uri to our result image file to yuri or not that one to yuri this one to string so this time we choose our our different theory from this file here and else we say results dot failure and i won't attach any error message here you can again use the same key for that and you're good however if if we do if we have a null image file here then we still also need to consider that case so this one here then we would simply return result at failure as well and we also won't do anything in the ui level with the error message from download worker i just wanted to show how you can do this and you will also know how you can access this value so if you want to show this then you you will know how this works so now we have our two workers um and the work manager knows how these work how they should perform that task however we did not specify that these should actually be executed in this order so first of all the download worker and then the color filter worker with the corresponding inputs so the rest will be now done in main activity where we specify our so-called work requests so we have a request to work for example to download our image or to put the color filter on our image and then we need to tell work manager hey please have this work this first work request with our given constraints please only download when there's internet and then have our second work request when um when the first one is actually finished and please only do the second one if the device actually has uh it's charging or whatever you can specify all kinds of work constraints here so first of all here in oncreate directly outside of set content we have our download request and we can do this using one time work request builder because what we do is a one-time work we don't want to download this image periodically there's also like a periodic work request builder that you can use um and that will then take the type of worker you have here so download worker for example and why does it give us an error uh do we need uh we need a duration parameter here so repeat interval that's basically the main difference between one time work request builder and periodic work request builder here you can just say okay i have duration a dot i don't know of millions of hours here for example every five hours i want this to repeat the download and then this will do this however we don't have we don't want this um just that you've seen this one as well we have a one-time work request builder of our type download worker doesn't take any parameters in the constructor because we don't have any repeat interval now we have a bunch of ways to manipulate and change this work request as i said we want to add some constraints and that has nothing to do with the constraint layout nope we will actually specify constraints here and we can do that using constraints builder and now there are a bunch of different constraints we could make sure that they are fulfilled before starting this request this download for example we can require that the storage is not low so if you download a very large file that makes sense to set this constraint so that it would not start this request if there is not enough space on the storage for the small image i don't see any issues um [Music] set required network type that's what we want to do and make sure that we actually are connected so what we do is network type connect it so you can also specify i want an unmetered or metered connection or roaming i'm not roaming rather but we want to say okay we just want to be connected if you have internet then we're ready to download the image let's see what we also have here set request charging so uh battery not low basically you can specify that if that is some kind of test that is a very battery heavy then you can specify that it's either charging or yeah not a low battery and then it will automatically start once it is charging so that's the cool thing about that but i think we just we're fine with that and we can just say build and what else do we have input merger that's something um you can use to specify how different inputs and outputs are kind of uh put together if they are overridden if they are um all capped so if you have the same keys basically as inputs and outputs then you can use that to specify something we don't need this here you can specify initial delay so you can say okay please wait 10 seconds and then start the request the one-time request and set back of criteria that's used to oops that's used to make sure what happens if you actually want to retry this so in what kind of interval do we do you want to retry this um do you want to do this consistently every uh 10 minutes or so or do you want to start with uh one minute then wait two minutes after that and stuff like that that's what you can do you can provide input data of course so if you want to provide data for your very first worker then you can use this and yeah there's a lot you can do set schedule requested at you can say okay at this specific time it should be scheduled so yeah i would just say we say build and leave it like that and then also do this for our color filter worker one time request builder and here we don't need any constraints so we can just say build then we can get an instance to work manager simply by using workmanager.getinstance passing our application contacts so it's simply a singleton in the end and that's how we get the instance now let's actually get to the ui and seeing how we now get the data from work manager to display it in our ui and to show the image so the way this works with work manager is that we have so-called work infos so that's a list of yeah work infos kind of so objects that contain information about a specific worker and we can get this by simply using our work manager instance get work infos for unique work life data that's a long name but that's exactly what we want um so we now need to provide a unique name here which i will use download for so the unique work here basically means that there's only one instance of this work of this task running at the same time so we don't want to have two downloads at the same time and this unique work is simply identified by this unique name download and we want to get these work infos in form of live data so we get the um consistent updates from work manager whenever anything changes whenever there's any new data that we can get we want to get that directly as an update and since we're uncomposed we don't want to deal with live data we can simply say observe as state so we have that whole thing as a compose state and can you can say that value which will give us a list of work infos cool and that now contains all the information about all of our um scheduled work manager tasks so let's see how we can in the end now get our two uries our color filter yuri in our download imagery we can simply first of all start by specifying a valve for the download info so first of all just want to find the corresponding work info that is related to our download request and then that is related to our color filter worker color filter request it actually is like this we can do this using remember providing a key here so whenever the key changes it will simply re-fetch this remember block so the key would in this case be work inverse so whenever work infos changes we make sure we re-fetch the download info and it would be equal to work infos that find we'll find the item where the id is equal to our download request dot id so that's really easy and then copy this do the same for the filter info i also when we fetch this when working for changes just here this should be our color filter request id and then based on these two infos from these we can now get the yuris and also theoretically our error message if we would want to get that so we can get the imagery here by a derived state off i will use which will kind of cache the result of this block here if you want to learn about these effect handlers in jetpack compose then i suggest you to click here where i go into that in much more detail than here so it will basically just re-fetch this expression here save it in image jury whenever it changes and then notify all of its observers so all kinds of composables that use this imagery that there is a new result so first of all the download yuri is equal to download info that output data that is the data object we now want to access and get string and the string that we want to get it has the key imagery and we want to get this as a yuri then we can take this again do the same for our filter yuri choose filter yuri here choose filter info here and then what we want to return in this block is well the area that we want to show in our image that is our filter yuri however if that's now so if we have not returned that yet uh retrieve that yet then we will potentially already have the download theory so the normal imagery which we then want to then want to display instead so in that case just download yuri like this and that's just now a normal compose state that we can use to observe the image yuri that we get from work manager and show that in our ui that we now only need to build and we of course need to launch our work manager requests so we'll start specifying a column here that will just fill the whole screen and we center the content horizontally and vertically then here we will have basically four items so first of all our image then below that our button to start the download then our text that displays the state of the download process and the state of the color filter process so if the image yuri is not equal to null we simply want to show an image given that yuri not a painter no actually we do want to have a painter when i say remember image painter which comes from coil and for the data here we can simply use our uri and it will display that on the description will be null here i don't care about that and the modifier will be modifier fill max with and let's add a little bit of spacing below that image height of 16dp import tp if we don't have an image then we don't want to show one so after this led block oops let's simply have our button when we click here we want to launch our work manager request so the download request so what we're going to do is we say work manager begin unique work so again um this unique work is just to make sure that we only have one of this one of this work type running at the same time and you can also see if you want to cancel work then you can also use work manager here cancel all work cancel work by tag you can also attach tags for work kinds of unique work given the unique work name by id all that stuff we won't cancel this here but if you need to do this then you're good to go and you're good to use this basically so begin unique work you can see that returns a work continuation so yeah that's a the name already says it so we can continue after that with more work and we want to do this we of course want to chain this with our color worker color filter worker let's oops let's start this unique work using our unique work name download which we chose before and well what's the existing work policy that is a policy that what should happen if there is already a work running with this unique name since it's unique there can't be two instances of this work running at the same time so i need to specify some kind of policy what should happen in that case and we will use existing work policy dot keep so you can see append would simply um just append this so when the first work finished i think it should go on with the second one you have replace a pen or replace i don't even know what that is right now um replace would simply replace the current work with the new one but we say keep which would basically if we launch download and the download is already running we just ignore the second download request and wait for the first one to finish and then here we need to provide a work request which is our download request and we can see we get a big warning because we don't call thank you that's what we always need to do but before that i want to say then to enqueue or to rather specify the next work request that executed after this one which is our color filter request and then we can say that in queue so that is pretty cool we just say begin unique work and then we do our color filter work and we enqueue all this so that's how easy we can now enqueue these work tasks and we can actually also specify enabled here for the button so whether this button is enabled or not this should be enabled only if the work is not running so if it's not active if if it is already downloading it we don't want to enable the button so we can say download info dot state is not equal to actually work info dot state running so that's how we can check if the work is currently running and in here we just have a little text for the button [Music] which just says start download cool and after this button the only thing that's missing are two texts let's have a little bit of spacer again off yeah let's say atp and then well the text is actually a conditional text based on the state of our work so when it's in queued we want to show hey works enqueued when it's blocked it's blocked when it's finished succeeded whatever we're going to show that instead so when in download info that state we'll use that and we'll use that again if that is work info state um running we say text is equal to download oh that's it downloading and then we can duplicate that quite some times pressing ctrl d we have succeeded download succeeded oops come on this then we have failed download failed cancelled then we have enqueued and blocked so enqueued would mean okay you called and queue and it's now waiting to be actually performed the task and we have blocked which would for example be the case if it is um if it should actually be executed but it is blocked for example by some kind of constraint or so so download blocked cool let's have another spacer below this and do the same for our color filter this time say filterinfo.state applying filter filter succeeded and let's replace this here very quickly and there we go that should be everything so i would suggest we simply launch this and there we go here we can start our download and i already noticed one thing i forgot and that is internet permission let's quickly change this in more manifest um use this permission internet like this quickly relaunch this there we go and let's hope for the best clicking start download you can see it's downloading filter is blocked so it still needs to wait until the download finished there's our image download succeeded applying the filter and oh that also worked cool so it seems like everything worked um we can also see that the image was saved on our file system if we go to this device file explorer here in on my android studio at least in the bottom right this can give us a little bit of yeah just information about the internal storage we need to open this data directory data again and now we need to find our package which is rook manager guide open this and here you have this cache we save this in our cache directory and here are two images so if we click double click this here we will see this image here in android studio as well and that's basically the proof that it really downloaded these to our storage let's actually remove or rather delete this app here and i will show you something else um let me quickly do this oops okay i am back i uninstalled my workmanager app here you can see unknown package it does not exist anymore so i will relaunch this and i will show you that if we now simply start the download and directly close the app and then wait a little bit like our 10 seconds then we should still see the image here because it should be all handled in the background so maybe wait a little bit and if we now say synchronize here's our cache and there are our two images so that works perfectly fine as well and that would even work if you if you relaunch the device and it is already scheduled then it would execute that task as soon as you get internet connection cool i hope you enjoyed this video which was probably rather long um but this really covered ninety percent of what you need all the time with work manager and the rest is very quickly um just a google search if you want to learn more about this async stuff like all the screen work here in our download worker retrofit color filter worker here without dispatchers and you really want to do this without making any silly mistakes then you want to check out this video which is about creatine mistakes that most people actually do and that can really ruin your project you
Info
Channel: Philipp Lackner
Views: 46,037
Rating: undefined out of 5
Keywords: android, tutorial, philip, philipp, filipp, filip, fillip, fillipp, phillipp, phillip, lackener, leckener, leckner, lackner, kotlin, mobile, workmanager, jobscheduler, reliable, work, task, jetpack compose, constraints, chaining
Id: Psc2xyutE2U
Channel Id: undefined
Length: 52min 27sec (3147 seconds)
Published: Sun Mar 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.