In this example, we will be see how to use implicit intents to performs actions using other apps(activities) in your android phone.
Steps to develop this can be summarized below -
Now, let’s begin the development...
We are using Intent class objects to start diferent activities in other apps. e.g. In the first case here, we are creating an Intent Object - in - and providing it with data such as Intent.ACTION_DIAL and phone number to be dialed. It should be noted here that different intent objects can be created in various possible ways. Like here we have used a simple Intent constructor to create an object while providing the action ACTION_DIAL and data(phone number) in the same constructor. We used Uri.parse method to provide the phone number. For other intent objects, like we can see in next 3 intents, we have used different methods. Also, one can create two different intent objects using different methods , yet performing same tasks for the end user.
In the below layout for the MainActivity.class, we have used a Linear Layout(having Vertical orientation) along with other Linear Layouts(having horizontal orientation). Wherever a button is requred, we have used an Image Button.
Couple of important observations here -
Below are the Screenshots for the output.
1. When the user enters a number and clicks on the call button , the intent object created is loaded with the input(the number provided by the user in edit text) using Uri.parse .We also check the availability of a dialer app in the users phone.
2. As the user decides which app should be handling the intent, the user is taken to the respective app where the number provided in the EditText is already displayed.
3. User entering a keyword in the edittext for search and then clicks the search button. In this intent object, we create the intent object in two steps. In the first step, we are providing the Action to be performed and in the second step we are provding the keyword to be searched. We also need to use a key here to provide the keyword(Searchmanager.QUERY).
4. When the user enters an email id in the email edittext and then clicks on the email button. In this intent object, we are providing the data by using .setData method and .putExtra method. putExtra method is being used thrice to provide email id ,Subject and email body content.
4. When the user clicks on share button , the intent object is getting created by using two methods - setType and putExtra. the setType method ensures that only the apps which can handle the text sharing requirement appear in the intent chooser for the user to decide.