Tip:
Highlight text to annotate it
X
Hello everyone! Lesson 31.
In this lesson we will learn what Uri and Intent-attribute
data, as well as call system applications (browser, dialer,
Map
We know that the Intent has an attribute action. With this
attribute is usually given to specify the action. For instance,
viewing or editing.
But the action usually is not done just so, and with anything.
So in addition to indicating the action, we must point to the
object with which to produce these actions. For this
Intent has the attribute data.
One way to set the value of this attribute
- The method setData (Uri data) the object Intent. The input to this method
served to Uri.
Uri - an object that takes a string, parses
on its components and keeps the information. The string
of course, should not be any, and prepared in accordance
in RFC 2396. Find a link to it in the description
the lesson. Uri has a bunch of methods that extract
parsed line of the individual elements.
I will create a Uri object from a string, and the log will display the name of the
method, and (through the colon) the value it returns.
For example consider the following line - http address.
We look, we return methods.
Concepts Scheme, Authority, Host, Path, etc. - are taken from the same
RFC document. There you can find a full description,
understand what they mean and consulted with the fact that we
returned Uri.
Consider another example:
FTP.
Coordinates
Here it turned out only select Scheme and SchemeSpecificPart.
Phone number.
Similarly, it came to select only two parts
of the line.
Contact from the address book.
In this example, the Scheme is content. This particular type of data
- Content Provider. It allows any program to give
access to their data, and other programs - reading
and change the data. This subject will be discussed later,
themselves will be creating this type of data.
See what standard Uri supported, you can
the link in the description of the lesson.
The examples show that the Uri is possible to create from
different lines: http-address, ftp-address, location, number of
telephone contact from the address book.
The content type can be determined by Scheme. And the same can Scheme
Intent Filter to customize and filter out Intent, only with the desired
data type in Uri, for example, only http. Thus, we have Let us
later, but for now we'll do a simple example in which
will form Intent with action and data, send it to
and watch what happens. Let's try to see the
the following: http-address, the coordinates on the map and open the window
dialing.
To see the coordinates on the map, you need an application
Google Maps. He is not in the standard Android platforms. We need
additions from Google. In the description there is a link to the video lessons
to the manual for their installation. After the update, you may
little to change the interface. Do not get lost.
Create AVD on a platform with the Google APIs API Level 10. Call
its up to you.
Create the project. Please note, we use the platform
Google APIs version 2.3.3.
Form the screen main.xml.
On the screen are three buttons. The first will open a web page,
the second - a map, a third - dialer.
Writing code in MainActivity.java.
I used three different ways to create a Intent-
and set its attributes.
If I used the designer btnWeb Intent (String action,
Uri uri). It creates an Intent and entry directly take action
and data. We use the standard system action - ACTION_VIEW.
This is a constant in the class of Intent - means that we want
see anything. As the data we provide to
Uri, created from a web link: http://developer.android.com. And if
try to describe in words our code, we get this:
Intent this means that we want to see the contents
this link and look for the Activity, which could help us.
In the case of btnMap used designer Intent (). He simply
creates Intent. In the following lines we assign
he attributes action and data. action - again ACTION_VIEW, as well as
data we create a Uri from a pair of coordinates. This Intent means
what we want to see on the map the specified coordinates.
In the case of using the designer btnCall Intent (String action).
At the entrance to it is immediately fed action, and the data indicated later.
action in this case - ACTION_DIAL - opens the dialer and
dials the number specified in the data, but does not start the call.
In the data - put Uri, created from the phone number 12345.
Three of these methods lead to the same result - Intent
filled with action and attributes data. Which one to use
- You decide depending on the situation.
Since our application will need internet to open
link and view a map of your Internet connection
the computer must be active.
Also in the application manifest file, the tab
Permission add a Uses Permission and right field
Name, select android.permission.INTERNET. This will give the application access
the Internet. Though I have reason and without it all
works ... It is not understood why.
All save and run the application.
Then, select the Web, the browser opens and displays the standard
content of the page to link to.
Go back, click Map. Displays the map. Zoom out
to understand what the coordinates of the places I used
Example.
Go back, press Call. Displays a set of standard screen
numbers and see that number, which we pointed out in
data, already dialed. We'll just have to press the call button.
Most likely, there are now a lot of questions like "What
would be if ... . " Some of them can not answer immediately
and I suggest that you experiment in the current application.
1) What happens if you specify the coordinates without the prefix
geo:
Rugnetsya system that has not found a suitable Activity
(See logs). Since Activity in the card set Intent Filter, which
(I think) is set to data c Schema = geo.
Similarly dialer will not work if you specify
number without the prefix tel.
2) What happens if you leave the coordinates geo, but the coordinates
specify the curves?
If we try to see as well, such coordinates
geo: a, b, then the card will run, but will tell us Unable to load the
URL. That is, Data came on Schema, but were incorrect.
3) What happens if the coordinates are correct, but the action to use
not ACTION_VIEW, and ACTION_EDIT.
It turns out that we want to edit the place
map given by these coordinates. But the system
tells us that it has not found such Activity. Therefore
Google Maps application that expects Intent with action = ACTION_VIEW and it can
show us your location. And for editing
it has been signed.
It should be understood that all of the applications in the system
sharpened by specific actions with specific
types. And if you try to call the
website address, or open the phone number on the map
- The system simply will not find application capable
on it.
Well, that's all for today, but for the next lesson, we write
a simple browser. Bye everyone!