Kotlin android fundamentals intents 400x277
  • Overview
  • Transcript

2.4 More Implicit Intents: Getting a Response Back

In this lesson, you’ll learn how to use implicit Intents so you can get data back from other apps. You’ll implement ACTION_GET_CONTENT and ACTION_SEND, in order to fetch an image into your app and to share content from your app.

Related Links

2.4 More Implicit Intents: Getting a Response Back

In the previous lesson we learned ACTION_VIEW and ACTION_DIAL. We saw how this action works with implicit intent. In this lesson we will see how to use ACTION_GET_CONTENT. Using this action, we will fetch the data in our activity. That is, we will get the result back in our calling activity using the implicit intent. We will fetch an image here from the device storage and show it in our main activity. And then we will see how to use ACTION_SEND. We will use ACTION_SEND to share a text and an image separately. So let's move to Android Studio and get started. So here in my activitymain.xml, I have added three more buttons. One is for getting the photo, which we will use for ACTION_GET_CONTENT. The second button I have added here is Send Text. We will use this to send message that is sending text message and the third button I have added here is to Send Photo. We will use this to send images. And then, for ACTION_GET_CONTENT I have added ImageView, where we will display the first image in our main activity. In my MainActivity.KT, I have created three functions. GetPhoto, sendText and sendPhoto. We will look into it one by one. Let us first deal with getPhoto for which we will use ACTION_GET_CONTENT. Since we want a result back in our activity, we will use startActivityForResult() for ACTION_GET_CONTENT. Let us write the code for it. Okay, so what I did here is first I initalized the intent and paused the action as ACTION_GET_CONTENT. And then, the type of content that I want is image. Object, if an app exists for doing the same, and if it does, then startActivityForResult because I want the image back in my main activity, and I want to do something with it. So startActivityForResult, and then I have paused the request code. That is REQUEST_GET_PHOTO. Here again, since it is a single request that we are generating from this activity, it doesn't matter what the value of this code is. So REQUEST_GET_PHOTO I have initialized it to 99. After that, onActivityResult. We need to overwrite this function which accepts three parameter, which we have already discussed in the previous lesson. We are checking if requestCode is same as the one that we have sent. That is, REQUEST_GET_PHOTO. Then the resultCode should be, RESULT_OK. If this condition matches, we will then start fetching the photo and doing something with it. We are fetching the photoUri, that is the image URI that we have got from the other intent. We are fetching the date property of intent and then in this variable, fetchedPhoto, we are fetching the bitmap of this photoUri that we got from the intent. Media Store.Images.Media.getBitmap(this.content- Resolver,photoUrl. And then, for image_view, I have set the setImageBitmap to the photo that we just fetched. And then, here the visibility of the image_view in activityMain.xml was invisible. And now since we have an image here we will set the visibility to VISIBLE. And once all this is done, we will return, and if this does not match, super.onActivityResult will be executed. Now let us run our application and see if we are able to fetch image and display it in our main activity. So now on hitting GET PHOTO button, Our file manager opens up. And here I have downloaded an image for the demonstration purpose. So on selecting this image. We get to display it in the image view. So we have successfully fetched the content. So this is how ACTION_GET_CONTENT works. Now we will work with sharing the text message from our application. To do that, we will use the action actionSend. Let us write the code for it. For ACTION_SEND, you'll be sending the text message. So the key here we can use is Intent.EXTRA_TEXT. And the value for this key will be Welcome to Intent Course at Envato. We will then have to set the type of the data that we are going to send, which is of text type. And then if there is some app to execute this intent, we will start the activity. Let us run our application and see if we are able to share text. So here our text message was, Welcome to Intent Course at Envato. Now let us try sending this text message. And here we get the list of application. Using which, we can do the same. So here is how the intent resolution comes to play. It has matched all the apps that can handle the ACTION_SEND_REQUEST. Similarly for my Android device, so this is my real Android device. On hitting SEND TEXT I get a list of applications that can handle this request. On selecting messages, just once, and here it shows me the contacts to which I might want to send the message. So here it comes up with the text written by default. So this is how ACTION_SEND works for sending the text message. You can get all this code from the GitHub and refer to it in case you face any issue. Now I would like you to implement the sendPhoto functionality. A hint I can give is, you can use this photoUri which we fetched from the getPhoto function using the ACTION_GET_CONTENT. And use the URI with this action and add the functionality to send it using implicit intent. I'll just form the base of it. Now you can use this photoUri in this function as well. Highlight the code for it in the reference code attached to the course, but I would suggest to try it out yourself first. That's all for this lesson. In the next lesson we will see how we can get our application in this app list to handle the request coming from other applications. So let's go.

Back to the top