Lesson 31. Why Intent has a data attribute. What is Uri. We call system programs

Lesson 31. Why Intent has a data attribute. What is Uri. We call system programs


In this lesson:

– we find out what Uri and Intent attribute data are
– call system programs (browser, caller, map)

We know that Intent has an attribute action. This attribute is usually an indication actions. example, revision or editing. But the action usually happens not just so, but with something. So besides specifying the action, we have to point to objectThese actions need to be done. For this Intent has an attribute given.

One way to assign the value of this attribute is to setData (Uri data) on an object Intent. The Uri object is fed to the input of this method.

Uri is an object to take line, disassembles its components and stores this information. The string, of course, must not be any, but compiled according to RFC 2396. Uri has a bunch methodsAllowing extract with scattered rows separate elements.

I will create a Uri object from the string, and in the log I will output the method name and (after a colon) the value it returns. For example, take the following line – http:

Uri uri = Uri.parse("http://developer.android.com/reference/android/net/Uri.html");

Let’s see what the methods return to us:

uri.getScheme (): http
uri.getSchemeSpecificPart (): //developer.android.com/reference/android/net/Uri.html
uri.getAuthority (): developer.android.com
uri.getHost (): developer.android.com
uri.getPath (): /reference/android/net/Uri.html
uri.getLastPathSegment (): Uri.html

The concepts of Scheme, Authority, Host, Path and others. – taken from the RFC dock, the link to which I gave above. There you can find their full description, understand what they mean, and verify that Uri has returned to us.

Consider the following examples:

FTP

Uri uri = Uri.parse("ftp://bob @ google.com:80/data/files");

(The code written above goes one line in fact. Here are the gaps around @ because of the markup features)

uri.getScheme (): ftp
uri.getSchemeSpecificPart (): //This e-mail address is being protected from spambots. You need JavaScript enabled to view it.: 80 / data / files
uri.getAuthority (): This e-mail address is being protected from spambots. You need JavaScript enabled to view it.: 80
uri.getHost (): google.com
uri.getPort (): 80
uri.getPath (): / data / files
uri.getLastPathSegment (): files
uri.getUserInfo (): bob

coordinates

Uri uri = Uri.parse("geo:55.754283,37.62002"); 

uri.getScheme (): geo
uri.getSchemeSpecificPart (): 55.754283,37.62002

Only Scheme and SchemeSpecificPart have already been selected here.

Phone number

Uri uri = Uri.parse("tel:12345");

uri.getScheme (): tel
uri.getSchemeSpecificPart (): 12345

Similarly, it turned out to select only two parts from the line.

Contact address book

Uri uri = Uri.parse("content://contacts/people/1");

uri.getScheme (): content
uri.getSchemeSpecificPart (): // contacts / people / 1
uri.getAuthority (): contacts
uri.getPath (): / people / 1
uri.getLastPathSegment (): 1

In this example Scheme is equal to content. This is a special type of data – Content Provider. It allows any application to access its data and other applications to read and modify this data. We will cover this topic later and create this type of data ourselves.

Here you can see what standard Uri is supported.

The examples show that Uri can be created from completely different lines: http-address, ftp-address, coordinates, phone number, contact from address book.

The content type can be determined by Scheme. And the same Scheme can be configured in Intent Filter and filter out IntentOnly with the type of data we need in Uri, For example, only http. We will do this later, and for now we will make a simple example in which we will form Intent with action and data, send it and watch what will turn out. Let’s try to look at the following: http, coordinates on the map and open the dialer window.

You need the Google Maps app to see the coordinates on the map. It is not available in standard Android system images (the ones you downloaded in SDK Manager). Requires an image that starts with “Google APIs”

Create an AVD on Google APIs with Level 10. API Call it your own.

Let’s create a project. Please note we are using the Google APIs platform version 2.3.3

Project name: P0311_SimpleIntents
Build Target: Google APIs 2.3.3
Application name: SimpleIntents
Package name: ru.startandroid.develop.p0311simpleintents
Create Activity: MainActivity

If you are unable to install Google APIs, then create a project as usual – with Android 2.3.3 platform. The Google Maps challenge in this example just won’t work.

form a screen main.xml



        
        
        

There are three buttons on the screen. The first one will be a web page, the second one will be a map, the third one will be a bell.

We write code in MainActivity.java:

package ru.startandroid.develop.p0311simpleintents;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

  Button btnWeb;
  Button btnMap;
  Button btnCall;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnWeb = (Button) findViewById(R.id.btnWeb);
    btnMap = (Button) findViewById(R.id.btnMap);
    btnCall = (Button) findViewById(R.id.btnCall);

    btnWeb.setOnClickListener(this);
    btnMap.setOnClickListener(this);
    btnCall.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    Intent intent;
    switch (v.getId()) {
    case R.id.btnWeb:
      intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://developer.android.com"));
      startActivity(intent);
      break;
    case R.id.btnMap:
      intent = new Intent();
      intent.setAction(Intent.ACTION_VIEW);
      intent.setData(Uri.parse("geo:55.754283,37.62002"));
      startActivity(intent);
      break;
    case R.id.btnCall:
      intent = new Intent(Intent.ACTION_DIAL);
      intent.setData(Uri.parse("tel:12345"));
      startActivity(intent);
      break;
    }
  }
}

I used three different ways to create an Intent and define its attributes.

In the case of btnWeb I used the Intent constructor (String action, Uri uri). he creates Intent and immediately accepts the entrance action and given. We use a standard system action – ACTION_VIEW. This is a constant in the class Intent – means what we want review anything. As given we submit the object UriCreated from the web link: http://developer.android.com. And if we try to describe our code in words, then this will turn out: this Intent means that we want to look at the contents of this link and look for an Activity that could help us.

In the case of btnMap the Intent () constructor was used. He just creates Intent. And in the following lines, we already assign attributes to it action and given. action – again ACTION_VIEW, and as given we create Uri from a pair of coordinates – 55.754283,37.62002. this Intent means that we want to see the coordinates on the map.

In the case of btnCall we use the constructor Intent (String action). He is immediately fed into the entrance action, and given to be specified later. action in this case – ACTION_DIAL – opens the caller and dials the number specified in given, But does not start a call. IN given – put UriCreated from a phone number 12345.

Three of these methods lead to one result – Intent with filled attributes action and given. Which one to use is up to you depending on the situation.

Since our application will need the internet to open the links and look at the map, it is necessary for your computer to have internet.

Also in the file manifesto apps, tab Permission add an object Uses Permission and right in the field Name Select android.permission.INTERNET. This will give the application access to the Internet. The truth is, for some reason, everything works without it … I have not yet understood why.

We all save and launch the application

Click the Web button

a standard browser opens and displays the contents of the page by link

Let’s go back, click Map. A map is displayed showing the location corresponding to the specified coordinates.

Let’s go back, click Call. The standard dialing screen is displayed and we see that the number we specified in the data is already dialed. All we have to do is press the call button.

Most likely, there are now many questions like “What if …”. Some of them I can answer at once and suggest you to experiment in the current application:

1) What if you specify the coordinates without a prefix geo:

The system swears that it has not found a suitable Activity (see Logs). Because Intent Filter is configured in Activity Map, which (I think) is set to data c Schema = geo.

Similarly, the caller does not work unless you specify a number without a prefix tel.

2) What happens if you leave the coordinates geo, But the coordinates indicate the curves?

If we try to look at such coordinates, for example geo: a, b, Then the map will launch but will tell us Unable to load the URL. That is, the data were fitted according to Schema, but proved to be incorrect.

3) What if the coordinates are correct but the action is not ACTION_VIEW but ACTION_EDIT.

It turns out that we want to edit the map location specified by these coordinates. But the system tells us that it did not find this Activity. Because the Google Maps app expects Intent with action = ACTION_VIEW and will be able to show us this place on the map. And it didn’t subscribe to edit)

It must be understood that all programs on the system are sharpened by specific actions with specific types of data. And if you try to call the site address, or open a phone number on the map – then the system simply will not find applications capable of it.

In the next lesson:

– writing a simple browser




Discuss in the forum [107 replies]

Leave a Comment