Lesson 27. Read action from Intent

Lesson 27. Read action from Intent

In this lesson:

– read action from Intent

In the last lesson, we made a clear example of how they are related action, Intent and Intent Filter. In this lesson we will continue the topic. There are two news: good and good)

The first news. Intent Filter may contain several action. Thus Activity makes it clear that it is capable of a few actions. For example, not only image viewing but also editing. It turns out that Activity can approach different Intent with different action.

Another news. ActivityWhich was called using Intent has access before this Intent and can read him attributes. That is, it can find out what action was used.

We will do the following: we will create Activity and configure Intent Filter on action = ru.startandroid.intent.action.showtime and action = ru.startandroid.intent.action.showdate. We hereby denote that this Activity is capable of showing both time and date. Next we will create Intent or with action = ru.startandroid.intent.action.showtime, Or with ru.startandroid.intent.action.showdate. they both will cause one Activity. And yes Activity knew to show him the date or time we would read action with Intent and determine it.

In general, now we will start to do and everything will become clear)

Let’s create a project:

Project name: P0271_GetIntentAction
Build Target: Android 2.3.3
Application name: GetIntentAction
Package name: en.startandroid.develop.p0271getintentaction
Create Activity: MainActivity

open main.xml and draw two buttons:


The screen is exactly the same as in the previous lesson.

code for MainActivity.java:

package ru.startandroid.develop.p0271getintentaction;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        Button btnTime = (Button) findViewById(R.id.btnTime);
        Button btnDate = (Button) findViewById(R.id.btnDate);

  public void onClick(View v) {
    Intent intent;

    switch(v.getId()) {
    case R.id.btnTime:
      intent = new Intent("ru.startandroid.intent.action.showtime");
    case R.id.btnDate:
      intent = new Intent("ru.startandroid.intent.action.showdate");

The code is also completely from the last lesson. We define buttons, assign them handler Activity, And summon Intent by clicks. Now we will do an Activity that will catch both of these Intent.

First, let’s create a layout file info.xml:


We have one TextView on the screen.

create ActivityLet’s just call it simple Info.

code Info.java:

package ru.startandroid.develop.p0271getintentaction;

import java.sql.Date;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Info extends Activity {

  protected void onCreate(Bundle savedInstanceState) {
    // получаем Intent, который вызывал это Activity
    Intent intent = getIntent();
    // читаем из него action
    String action = intent.getAction();
    String format = "", textInfo = "";
    // в зависимости от action заполняем переменные
    if (action.equals("ru.startandroid.intent.action.showtime")) { 
      format = "HH:mm:ss";
      textInfo = "Time: ";
    else if (action.equals("ru.startandroid.intent.action.showdate")) { 
      format = "dd.MM.yyyy";
      textInfo = "Date: ";
    // в зависимости от содержимого переменной format 
    // получаем дату или время в переменную datetime
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    String datetime = sdf.format(new Date(System.currentTimeMillis()));
    TextView tvDate = (TextView) findViewById(R.id.tvInfo);
    tvDate.setText(textInfo + datetime);

We get it Intent using the getIntent () method, we read from it action and, depending on the value, form and display text.

Do not forget to register a new Activity in manifestos and create it Intent Filter with two action and category. AND label specify Date / time info.

Intent Filter for Info contains two action. So if it comes Intent c any one of them Activity will called.

We’ll save everything and run it.

Click the Show time button. Most likely you will see the following:

The system offers us a choice. That is Intent with action = ru.startandroid.intent.action.showtime found two relevant Activity. That Date / time info we just created, everything is clear here. The question is where the second one comes from IntentFilter. The answer is an ActivityTime from the last lesson. it is called IntentFilter because we didn’t spell out in the manifesto last lesson label for this Activity and the default system displays the name of the program.

If the system did not display the selection dialog, then you either did not create an application from the last lesson, or rebuilt AVD, or somewhere in the code.

we choose Date / time info and we see what is just a cod. Activity determined that Intent was with action = ru.startandroid.intent.action.showtime and showed time with the text Time:

If you choose IntentFilter we will see Activity from last lesson:

Now let’s try to show the button Show date: we see the following choice:

We see ours again Date / time info and two Activity with the past lesson. They contain everything action = ru.startandroid.intent.action.showdate in Intent Filter and we have to choose. let’s choose Date / time info and see the date with the text Date:

If you choose Date basic or Date extended see what they did in the previous lesson:

the usual date

and advanced

We saw that one Activity can be called with Intent with different action. Activity can read action and take the necessary steps.

We also made sure that Intent looking for Activity on all programs in the system. This time he found Activity from the program we did in the previous lesson.

In the next lesson:

– transmit data using Intent

Discuss in the forum [21 replies]

Leave a Comment