Intents & Intent Filters - Android Basics 2023

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to a new video in a new episode of Android basics in this video I will talk about intense and intent filters so let's get straight to the point what is an intent because that is really a core concept of whole Android development in the end you can think of an intent like an envelope which is used to transfer an intention of your app that is what's called intent and transfer that to a different Android component and in most cases that different Android component is just an activity but it doesn't need to be an activity we will also get to other Android components later in this Android Basics uh playlist so for example services or sending an intent via a broadcast is also a thing but that isn't covered yet and I will talk about that in more detail in a later video here so in short a nintendent is just a way to communicate with a different Android component in our case and activity and when it comes to intense we distinguish between so-called explicit intents and implicit intents explicit intents on the one hand are targeted towards one specific app that has to be specified in that intent so that could on the one hand be used to just launch an activity in our own app since then that intent is used to launch the activity of this specific app our own app in this case or we could also use an explicit intent to launch an activity of another app so if we are in our very own coded app here but we would want to launch for example the YouTube app of our Android device and then we can also do that with an explicit intent so in that case we will specify the intention we want to launch the YouTube app and when we then fire and send that intent Android will also launch that YouTube activity so the YouTube app in the end but one step at a time let's start with the most simple scenario that we just want to launch an activity of our own app because we haven't dealt with that yet in this playlist how we actually launch a different activity of our own app for that we just want to go to our root package here and create a different activity class so we can create a new kotlin class here we can just call this for example second activity like this and this would also inherit from component activity and then we really don't need to put a lot of content in here we just want to overwrite oncreate and then have our set content function from jetpack compose I'm going to call our theme so we just have a consistent Style on our app and then we can have just a text at this place second activity so we just see that there's a change in our screen and whenever we add such an Android component like an activity to our app we also need to register that in the so-called Android manifest and I haven't talked about the Android manifeston yet but that is really just a place which summarizes the functionality of your app so it basically shows to the outside this is what your app needs to do and these are the components it consists of these are the permissions it needs so for example if it needs to access the user's camera then you also register that in the so-called Android manifest and when we just create such an extra activity in our app we need to put that and register that in our manifest which we get to we click on manifest and then this is a default manifest we'd only need to take a look at that in detail at the moment but as you can see here's already a Roman activity which was put in there by default when we create a new project but apart from that main activity and all these options here we can just go below this activity block and just add an extra activity here and that it already suggests the name second the T we can close this and then we're good to go and in our main activity I already put in a column and a button so we just have a centered button or screen and when we now click this button we want to create an intent with intention to launch our second activity and then finally fire that and actually launch it so let's take a look how we can do this so in here in this unclick Lambda we ought to have an intent object from Android content and here are quite a lot of overloads of how we can create such an intent so we could either specify an action which I will go through later in this video or if we want to launch an activity of our own app then we are interested in this package context and class overload the package context is just the context of your old app or this activity in this case we could also pass the application context that doesn't matter and the class is just the class of our second activity so double colon cluster Java that is how we create a Nintendo object that is a very simple way to create it and now we can say that also and then we call start activity that's a function that is available in activities so we can also obviously start activities from within activities and you can see that function requires an intent object which we now have here so we can just pass it and if we Now launch this app then we see something like this if we now click our button and then we get to our second activity so that is just a separate screen now as I said in an earlier video you usually in pure jetpack composed products you don't have multiple activities but what is now interesting is that this activity does not need to be from your app as I said because we can also launch other activities as I mentioned in the example with the YouTube app so that is also what I want to show you how we can now open the YouTube app from within our app after button click and in order to launch one specific app that is installed on the device we need its so-called package name so each app has a package name in this case you can find it up here so compare coding intense and intent filters is my package name and the same way the YouTube app also has a unique package name so let's minimize that comment out this intent which launched our own activity and let's now create an intent that would launch the YouTube app so we create that here intent and now which overload do we choose here because we can't really use this package context and class anymore because we don't have a reference to the class of the activity of the YouTube app here because it's a completely different app of course instead what we do is we pass a so-called action and in this particular case we want to pass intent that Action main this is basically just an action that specifies what we want to do with this intent and in the iron an action is just a string so that the receiver of this intent in this case the YouTube app knows how to handle it and what to do with this intent in this specific case in our Action main just refers to hey I want to launch the main activity of that app so the initial activity that shows up when you launch this app you can then also say that also and we could not just say star activity with our intent again but right now Android wouldn't know where to send this to because we just specified hey we want to launch the main activity but of which app so we need to kind of configure this intent a little bit more and in this specific case we want to assign the package so we can set the package name of the app that we want to send this intent to so in that package is equal to well what is the package name of the YouTube app there are multiple ways to find out the package name of a specific app so it's on the one hand contained in the URL of the Google Play link but that is a bit complicated so we don't want to go to Google play right now luckily we have a tool called ADB the Android debug Bridge which is just a way to communicate with an Android device such as our emulator and we can use that just to print all package names of all apps that are installed on this emulator we can simply use this by going to terminal down here we then say ADB shell to open the ADB specific shell and then we want to say PM for package manager we want to list all packages if we just press enter here then we get all the package names of all apps that are installed here um obviously those are quite a lot and not those are also more than the app icons you see because there are lots of apps that don't show as a specific icon you can already see the package name of the YouTube app here but if you are actually searching for a specific name and you can't find it in this huge list you can also filter by saying PM list packages and then you say you forward this result to grab which is a command line tool to search for specific strings for example for YouTube and then suddenly we only get YouTube music here and the YouTube app so we are interested in this package name we want to copy this like this command C and then we paste it here in this package name and this would Now launch the YouTube app however there's one more thing I would recommend you to do and that is actually checking if this app is installed on the device for the YouTube app it's quite likely that it is installed because it's just installed by default on every Android device but if you're launching an app that is not installed by default then you of course need to check if it exists otherwise the app will crash so in that case we can either wrap this start activity function inside of a try and catch block or we could say we have an if statement and we check if this intent that resolve activity passing in the package manager is not equal to null then we want to launch this activity this is a function we will actually make use of later in this video um for just launching an activity with a specific package name I would rather just use the try catch approach because you can see um it gives us a warning that we should add a queries I will get to that lecture later in this video um so let's just take the start activity of function and we put that in a try catch block and then that is also fine we want to catch activity not found exception and down here we could handle that case I will just print the stack trace and if we Now launch this apron or emulator take a look here wait until it's booted up then click on our button we can see that our YouTube app is opening so that is working perfectly fine so those were two examples of explicit intents because we explicitly mentioned an action or a thing we want to do with that intent because it's only sent to one specific app either to our own app to launch an activity or in this case to the YouTube app however then there's also a different type of intense which is called implicit intents implicit intents on the other side just specify an action so something you want to do with that intent and then Android will check which apps can actually satisfy that intent and show the user a Chooser so they can choose which app they want to kind of send that intent to or which app they want to open an example is if you would want to open a text file in your app but your app itself can't do that because you don't have the code for that to actually Powers it to read it to show to the user to maybe perform it so you rather want to send the user through a different app that can open a text file but you don't really care about which of that is you just want to have an app that can open a text file then you use an implicit intent because you just specify the action I want to open an app that can open a text file or another use case would be you want to send an email because you likely don't want to implement a whole email client in your app you rather want to just open an email app like Gmail and you want to pass all the information that you want to have in that email to that intend so Gmail can handle that so if we go back to our code minimize this here and we also comment on the second intent and we now create a third one an implicit one in this case like this and this time we just specify a different action so in hand that action you can see there are tons of actions we can actually send and you can also specify your own ones of course those are just strings in this case if you want to send an email that is called action sent so I want to send something then we can say that apply and in here we first of all want to specify the type so this is just the mime type if we want to send something from the send data we need to specify this kind of data is actually the data we sent so maybe if you want to send a photo then you want to specify hey I'm actually just sending images here so the mime type would be our image or in this case it would just be plain text since we're sending an email so that is what we will do Text slash plane and now what we haven't covered yet is how we can also attach data to intent because before we just launch an activity with our intent here or we just launched the YouTube app with our intent but what if we now also want to pass some parameters to that intent so the receiving app in this case Gmail or any other app the user chooses to send an email also knows okay that is the subject line that is the email address I want to send this to that is the email content in that case we call that so called intent extras so we can attach as many extras as we want by just saying put extra we need to give it a name which we can then later use to retrieve that extra and for such email apps that are specified extra names which in this case would be intent extra email this is done an array of since we could also have multiple recipients of that email but let's just have test at test.com for example to send this email to you we can then duplicate this two more times let's also have an extra subject here we just want to have a string for the subject of the email um this is my subject for example and we have an extra text which is the actual email content and in here we can say this is the content of my email and the same way we pass this extra serial to this implicit intent for the Gmail app in the end or any other mail app we can also of course pass these extras here for our first intent if we want to receive some parameters on our other activity in that case just very quickly if we are on our second activity then we also have access to an intent object here which is nullable so if there is an intend or if this activity was launched with our intent then you can also receive the extra Seer from this intent by just saying intent dot get string extra for example if you attach the string and then you just put in the name of that extra and you get back the string of it but is really the same mechanism here just that this is an implicit intent and we could potentially along different apps with this and every now we save this intent in a variable we can now check with the previous check I showed you if intent.resolve activity passing in our package manager is not null that is how we now check if there are activities or other apps that can't satisfy these requirements because if there are no email apps then there is no app that can actually open that can be open from our app here so if that is the case if we have apps that can satisfy this intent then we want to start this app with this activity with our given intent and now what actually is this warning because right now this won't work the reason we kept this warning is that from some kind of Android version I think is 10 or 11 Google wanted to limit it that apps can scan for other apps so basically that your app can scan which app can open a specific um specific type of file for example and they only want your app to be able to scan for other apps if your app explicitly queries that so if your app explicitly needs to open another app for example to send an email but if your app only is to open another app to send an email then your app shouldn't be allowed to query which apps can open an image for example that is why Google wanted to limit this and while we get this warning here to specify these queries we need to go back to our manifest go down here and we specify such a queries block very simple here we just specify the type of intent action we actually want to send so we say intent and in here we want to specify the action in this case it's action sent that is what we're going to query for so we want to query four other apps that can receive intents with this specific action and we can also specify a data block for our mind type which is text plane like this if we Now launch this take a look here on our emulator and then click on our button then something will happen Okay we actually get a Chooser where we can choose which app we now want to open here so you can see Google Drive will also satisfy these requirements that doesn't have to be to send an email but if we now tap Gmail then we get to the Gmail app and after the moment you can see here are our email address we're going to send this email to we see our subject and the conduit and then we can just edit this from within Gmail and also send it from within Gmail and if you would for example now have the Yahoo app installed to send emails and that would be your app of preference then this will also show up in this Chooser to send an email so that should clarify what intents are but what is actually if you want to kind of register your app to receive an intent because till now we just send out intents but what if you actually build such an email app and you want that your app actually pops up in this app Chooser where users can choose an app to send an email with and then we get to so called intend filters because with an intent filter we can specify exactly that that our app can receive specific tabs of intents and in this case I want to show you that with a simple example that if we go to Google Chrome and here we long click on an image for example then we can also Click Share image and if we do that then Google Chrome will send an intent and implicit one that it wants to share an image and you can see here it will also show some actions you can also go to more but that will now show all apps that can accept such images and I want to now show you how we can implement or how we can set up our app to receive this image so we can hit share image in Chrome and then directly shoot that image in our app and for that we again need to go to our Android manifest and specify this intent filter so with that we can specify this is the action our app should actually receive the main action is the one we've used so far because when the user initially launches your app what will happen is the Android system will send an intent with this main action to your app so it will just launch the main activity and we can just specify as many of such intent filters as we want so just a new one here we specified in the action in this case it's called sent again because the Google chroma would send that image to another app we want to specify a so-called category these are just different categories your app could potentially fit into so for example if you have a contacts app if you have an email app we just have a default app here and we don't want to explicitly specify that and now we specify in data block so what kind of data our app should be able to receive we can specify the mild tab in this case we want to receive images so we don't want our app to show up if the user is about to share some text note only if they are about to share some images so image slash asterisk if you only want to receive a specific type of image then you could say image.jpg image.png but we just want to receive all images and then we close this off and now we registered our app to receive such intents and if we Now launch this we should already see our app when we go to Google Chrome so there we go if we now go back to Chrome and one click on an image go to share then go to more then you can see here suddenly our app pops up if we now click that then our app will obviously open but we don't see an image right now because we don't receive that intent yet push the Google Chrome app now sent to our app so obviously Chrome needs to put that data of that image inside that intent which it now sends cover app but our app of course needs to Powers it and show that image in the end and by default what Google Chrome will do is or what that new intent will do it will launch a completely new task of our app so if our app is already open it will launch a new instance of our app which will then get that intent here in oncreate so we could get that with intent and then just retrieve these extras what makes more sense in this case is to make use of these launch modes which are talking about in an earlier video so here for our activity we can specify launch mode single top which means if our app is already open we will just take that active instance and just send the data to that active instance so in that case if we set the launch mode to single top then what will happen if we go to our activity it will trigger this all new intent function instead because obviously not a new there is not a new activity that is created so on create wouldn't be called again but at this all new intent callback will be called in that case delivering this intent which comes from chrome and here we can now get the so-called Yuri so just the um address of that image on our device which you can then read in as a byte so we just want to retrieve this with intent um get parcelable extra so parcel levels are just a way to um package or serialize some data that is not a primitive data type and in this case it's just called intent.extra stream here you always need to research a bit how the extras are called in that specific case um but usually you'll find that out quite quickly and we also want to specify the class of that that is a Yuri double colon class at Java now this overload of the get personal extra function is only for API level 33 so we want to add a check here I'll enter surround with a version check and if we are not on a device with a minimum level of 33 we want to not have this Yuri here and only use this one you can see it's deprecated but only on the latest Android version so now we have the Yuri if that intent actually contains it with our detect the theory and display it in an image that we could put in here in our UI since we kind of get that theory in a different function than this on crate function here we need to do this via view model that will update the state of our composables in this case um so it's pretty simple in this case we want to go to our root package create a simple image view model inherit from viewmodel and if that is new to you then please watch the video about view models here in this Android Basics playlist and we want to have just a VAR Yuri which is a nullable Yuri by mutable state of not initially I'll enter to import that we can then say that as a private set so we can only mutate that from within this view model and we want to have a function to update the theory with a new one when we get that new intent we can just say this that theory is the new Yuri we can then go back to our main activity create an instance of our review model here with the way I showed in the previous one value model by view models this one here specifies an imagery model and then we can scroll down to our new intent function and actually submit that theory so viewmodel dot update jury with the new era here that will update the Yuri here and since it's a compose state it will also update any composables that useless Theory so in this case let's put an image above our button and just to keep this a bit more simple I want to introduce an image loading dependency here so I'm going to go to gradle's grid module app scroll down and add this dependency from coil so coil is an image loader an image caching Library which is very commonly used in composed projects because if we wouldn't use that then the process of actually reading in such a Yuri and converting it to byte is a bit more complex and I don't think this fits well into this video here which is more about intents so make sure you synchronize Gradle after adding this dependency then you can have an async image which comes from coil and the model would just be our Yuri so viewmodel dot Yuri and let's also add a check here and that the Yuri actually exists so it's simple null check and then we have the model and the content description let's just put null in there to keep it simple and if we now launched this we should hopefully see a working example of how what I want to show you there we go obviously nothing is showing because our app now wasn't launched with an intent that contains in this data this this image but we want to go to Chrome now long click on an image again and then there's a little shortcut that we can open our app with that Chrome app so let's click that and we get to our app and there is the image so it is working if we go back and open another image you will see that this will work just fine as well a little Chrome is quite laggy here um let's open our app again with a different image and then there is the different image and obviously this doesn't only work for images but for all kinds of things that another app would share with your app so maybe you have a phone calling app where your users can make calls with then of course you might also want to register your app as such a phone calling app so that when the user is about to make a call they actually get to choose if they want to use the default app if they want to use your app another phone calling app because otherwise it's quite likely your users won't use your app if you don't make it easy for them to use so I hope this video went into quite a lot of detail about intense and intent filters and you now fully understand that topic I would recommend to play around with that a little bit and also try out different use cases of intents we will still use these later in this playlist for other use cases but you now understand the basics of these and I want to thank you for watching this far if you like this and you haven't subscribed yet then definitely do because then you won't miss these two new videos every single week about Android Basics but later on also a lot more advanced Android topics which my channel is pretty much full of already so thanks for watching see you back in the next video and have an amazing rest of your week bye bye [Music] thank you
Info
Channel: Philipp Lackner
Views: 58,881
Rating: undefined out of 5
Keywords:
Id: 2hIY1xuImuQ
Channel Id: undefined
Length: 25min 36sec (1536 seconds)
Published: Wed Jun 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.