Skip to main content

Using implicit intents to create activities in Android

Before starting the development activity, we will define the concept for the use of "implicit Intents" on mobile solution (Android).

The implicit Intents are used when a component is not specified exactly to use, instead, you specify the functionality required by an action (for example, 'view', 'make search', 'check') and data (For example, the URL of the page you want to see, the phone number you want to dial), and Android should determine the best component for use. This is one of the interesting features proposed by Android. This functionality allows an application to trigger an "Intent", without knowing exactly what the application or component that will receive it.
The most commonly used filter is an action, and the most common action, has to do with how "ACTION_VIEW. "

The basic steps to launch an activity using an "implied intent":
  • Declaring the "intent" with the appropriate action, for example, "ACTION_VIEW", "ACTION_WEB_SEARCH", etc.
  • Attach additional information required, as the action of "intent".
  • Launch the intent (let the system find the most appropriate activity).
We will generate a sample program that allows us to understand the mechanism of using the "Intents" in Android.
The "Intents” are widely used in all mobile developments generated in Android, as I commented, we choose to use them explicitly or implicitly. The second option, is selected for the development of this article.
Simple example using implicit Intents
First, let's create a simple activity that allows us to respond to an event. The idea is a simple GUI with a button that allows the release of several "Intents" useful. Normal situation for mobile development.

We will have to resolve these situations:

- Navigate to a Web site.
- Open the contacts in my directory and browse them.
- Start a call with a specific number.
- Carry out a Google search from a search string as a parameter.
- Run a voice command.

In each of the declared activities, use a handle on the click of a button to launch the "intent".

Button btnLanzar = (Button)findViewById(R.id.button1);
       btnLanzar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

}
});
Code 1 -Button Manager "btnLanzar".

Use “startActivity(intent)" to start the activity. This operation belongs to the package "android.app" particularly to the class "Activity". This operation initiates an activity as an essential feature to highlight, this activity does not receive information when closing.

You can extend this information in the following official link.Ver.

As a parameter, the operation “startActivity" generally take an " intent" and any other data. Consider the structure of an "intent".
The "intent" belongs to the package “android.content”. We can say that an "intent " is an abstract description of an operation to be performed. Its most important use is the start of activities, which can be seen as the link between activities in the Android mobile solutions.

The main components are:
- An Action
- Data (expressed as URI)

You can extend this information in the following official link Ver.

Navigate a Web site

In this example, we create a new "intent" with action"ACTION_VIEW. " As data pass the web address "http://www.google.com/ ".


startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com/")));
Code 2 - Launching an "intent" to navigate a website.

An important point emphasize in this scenario is, we're not necessarily throwing the browser of the system, what we are doing is sending the request to the system that we want to see the page "http://www.google.com. ". The system will take the decision. Define what the most appropriate application, which may be the browser of the device or perhaps another application that the user has installed. Could be another Browser (Firefox, for example). For that reason the "intent" is named "implicit. " We are not stating aspecific application or component.

Operate the contacts from my directory.
In this example, we create a new "intent" with action"ACTION_VIEW"and pass data "content://contacts". The goal is to perform the operation display contacts from my directory.


startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("content://contacts/people/")));
Code 3 - Launching an "intent" to open the contacts in my directory and browse.

A emphasize, "content://contacts/people/" is a URI that represents the collection of contacts in the directory.

Start a phone call.

In this example, we create a new "intent" with action"ACTION_VIEW" as data pass "tel: 73647832. " This will place the  application on stage phone call to confirm the call to the number represented.


startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("tel:34247")));
Code 4 - Launching an "intent", to use the telephone interface.

Carry out a search in Google.

In this example, we use the creation of an "intent", but performed separately initially and then to insert a "Query" to it. There are some"Intents" that need more than one action and a URI. These additional data are called "extras" and can be added with "Intent.putExtra" as the example of the Code 5.

Intent intent= new Intent(Intent.ACTION_WEB_SEARCH );
intent.putExtra(SearchManager.QUERY, "Bortolotti");

startActivity(intent);
Code 5 - Launching an "intent", with a web search.

Launching a voice command.

In this example, we create a new "intent", with action"ACTION_VOICE_COMMAND"


startActivity(new Intent(Intent.ACTION_VOICE_COMMAND));
Code 6 - Launching an "intent" to make a call to a voice command.

Video of implementation

View (In spanish)

View  (In English)

Implementation source code

Download

I hope this will be useful.


References

- Sitio Oficial Android, http://developer.android.com/reference/, 2011.
- Manning Android in Action 2nd.Edition - W. Frank Ableson, Robi Sen, Chris King, 2011.
- The Android Developers Cookbook - Steele and To, 2010.

Comments

Popular posts from this blog

Google Assistant, simpleness of interaction to call a webhook

The creation of an application using Google Assistant is very simple and represent a big impact to interact with users in diverse environments and particularly in home Hey Google ...Where is my dog? If we plan to connect a security camera system, the challenge of finding my pet in an automated way is more feasible, then I will give more details of how I intend to do it. Now we are going to prepare the functionality of the assistant’s requests. To create an agent and interact with Google Assistant , we will use DialogFlow . Let's see the main elements to create an agent, and using this agent call a webhook with some functionality. * Part of the objective will be to try to prepare the logic of interaction with a home security system * Arlo System From the DialogFlow console, we will create a new agent: We can customize different support languages, for this case I will use English to facilitate the testing of the agent. [You can include Spanish and also go in...

SMI Developer Day - Session 2 was pure developers adrenaline

A new session of SMI Developer Day program has been conducted at the Cultural Center Borges in Buenos Aires. The event was pure adrenaline to the ecosystem of technology development in Google. A great attendance and I could feel pure adrenaline at the conference on May 8 at the Cultural Center Borges in the context of SMI Developer Day event . In this opportunity we present an approach to development Web solutions , innovative technologies such as HTML5 and present an option for persistence in disconnected scenarios. A great conversation environments with a lot of questions, showed the state of motivation of the developer community in the region. A region full of talent. A region that every day becomes more involved in technology , mobility , computing in the cloud. Furthermore, this region strengthens a vision of entrepreneurship, and drive an amazing ecosystem. An excellent shared moment with the community ...

Are you interested in creating a TensorFlow SIG?

If you are interested in contributing with TensorFlow, a very interesting option is the proposal of SIG. TensorFlow hosts Special Interest Groups (SIGs) to focus collaboration on particular areas. What is a TensorFlow SIG? SIGs do their work in public. The ideal scope for a SIG meets a well-defined domain, where the majority of participation is from the community. Additionally, there should be sufficient evidence that there are community members willing to engage and contribute should the interest group be established. More information here,  https://www.tensorflow.org/community/sig_playbook If you have ideas, and you want to participate with me into this initiative, here I have a open communication channel to start the analysis of the proposal: https://tensorflowexperiences.nicolasbortolotti.com/main/sig Esta obra está bajo una Licencia Creative Commons Atribución-CompartirIgual 4.0 Internacional .