Tutorial details

Lesson 27. Reading action from Intent | App Code for Sale | Preview

Lesson 27. Reading action from Intent | Android Tutorial

Reading action from Intent

Overview PAGE TOP

In this lesson:

-reading action from Intent

In the previous lesson we have created an obvious example that illustrates how action, Intent and Intent Filter are bound together. In this lesson we will continue illustrating the topic. I have both good and good news for you )

News item one. Intent Filter can contain several actions. By doing so Activity notifies us that it can perform several actions. For example, not only viewing an image but also editing it. Activity can approach different Intents with different actions.

News item number two. Activity which has been invoked using Intent has access to this Intent and can read its attributes. That is, Activity can find out which action was used.

We will do the following: create an Activity and configure Intent Filter for action = ru.startandroid.intent.action.showtime and action = ru.startandroid.intent.action.showdate. By doing so we specify that Activity is capable of displaying both time and date. Later we will create an intent with action = ru.startandroid.intent.action.showtime or with action = ru.startandroid.intent.action.showdate. They will both invoke one Activity. And for Activity to know whether to display date or time, we will be reading action from Intent and define what to show for each action.

Okay, we will start implementing it and everything will be clear.

Let’s create a project:

Project name: P0271_GetIntentAction

Build Target: Android 2.3.3

Application name: GetIntentAction

Package name: ru.startandroid.develop.p0271getintentaction

Create Activity: MainActivity

Open main.xml and draw two buttons:

<?xml version="1.0" encoding="utf-8"?>
 android:text="Show time">
 android:text="Show date">

Screen is the same as in the previous lesson

MainActivity.java code:

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");

Code is also completely the same as in the previous lesson. Define buttons, assign a listener - Activity and invoke an Intent on button click. Let’s now create an Activity that will catch both these Intents.

To begin with create a layout-file info.xml:

<?xml version="1.0" encoding="utf-8"?>

We have only one TextView on the screen.

Create an Activity, name it just Info.

Info.java code:

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 receive Intent using getIntent() method, read action from it and depending on its value we display text on the screen.

Don’t forget to register new Activity in the manifest and create Intent Filter with two actions and a category. And specify a label - Date/time info.


Intent Filter of Info contains two actions. It means if the Intent with any of these actions comes - the Activity will be invoked.

Save everything and run the application.

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


The system gives us a choice. That is, Intent with action = ru.startandroid.intent.action.showtime found two Activities that fit. The one that is Date/time info is the one we have just created, everything is clear with it. The question is where does the second item with name IntentFilter comes from. Answer - this is ActivityTime from the previous lesson. It is named IntentFilter because we have not specified a label in the manifest for this Activity in the previous lesson and the system displays application name by default.

If the system haven’t displayed a choice dialog, that means that you didn’t create the application from previous lesson or you have created a new AVD, or there is a mistake in your code.

Choose Date/time info and we can see what we’ve just implemented. Activity recognized that Intent was with action = ru.startandroid.intent.action.showtime and shown time with Time: text


If to choose IntentFilter we will see Activity from the previous lesson:


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



We can again see our Date/time info and two Activities from the previous lesson. They all contain action = ru.startandroid.intent.action.showdate in their Intent Filter and we have to make a choice. Choose Date/time info and you will see date with Date: text:


If you choose Date basic or Date expanded we will see what we were doing in the previous lesson:


and expanded


We have figured out that one Activity can be invoked using Intent with different actions. Activity can read action and perform the required operations.

We have also made sure that Intent looks for Activities in all applications in the system. In this case it found Activity from the application that we developed in the previous lesson.

Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.