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":
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.
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).
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".
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.
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) { } }); |
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/"))); |
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/"))); |
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"))); |
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); |
Launching a voice command.
In this example, we create a new "intent", with action"ACTION_VOICE_COMMAND"startActivity(new Intent(Intent.ACTION_VOICE_COMMAND)); |
Video of implementation
View (In spanish)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
Post a Comment