Lesson 26. Intent Filter - Practice

Lesson 26. Intent Filter – Practice


In this lesson:

– Call Activity using implicit call and Intent Filter

Recent lessons have been congested. This theory must be understood so that there are no problems with practice. These topics are the basis – Task, Lifecycle, Intent. If something is unclear, you can always re-open and re-read the material. In the following, we will implement examples that will confirm this theory and make things clearer.

We have learned in the past lessons cause Activity using Intent and explicit instructions class. We also know that there is another way to call Activity – implicit. It is based on the fact that Activity is called not by name, but by functionality. That is, we want to perform certain actions, create and configure the appropriate Intent and send it Search those Activities that could handle our mission.

Let’s see how this is put into practice. We will create an application that will show us the current time or date. We will do this with the help of three Activity:
– the first will contain two buttons: Show time and Show date
– the second will reflect the time
– the third will display the date

Clicking the Show time button will cause a second Activity, and clicking the Show date button will cause a third Activity. but we realize we do this not through the direct indication of Activity classes in Intent, but through Intent Filter.

Let’s create a project:

Project name: P0261_IntentFilter
Build Target: Android 2.3.3
Application name: IntentFilter
Package name: en.startandroid.develop.p0261intentfilter
Create Activity: MainActivity

open main.xml and draw two buttons:



    
    

writing the implementation MainActivity.java:

package ru.startandroid.develop.p0261intentfilter;

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. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btnTime = (Button) findViewById(R.id.btnTime);
        Button btnDate = (Button) findViewById(R.id.btnDate);
        
        btnTime.setOnClickListener(this);
        btnDate.setOnClickListener(this);
    }

  @Override
  public void onClick(View v) {
    Intent intent;

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

In the code, we defined the buttons and assigned them Activity as handler clicks. In the method onCilck we determine which button was pressed and we create Intent.

To create Intent we use the constructor: Intent (String action). That is, when we create, we fill in an attribute of an object called Intent action. This is a regular string constant. Action usually indicates actionWhich we want to do. For example, there are the following system action constants: ACTION_VIEW – view, ACTION_EDIT – edit, ACTION_PICK – select, ACTION_DIAL – make a call.

If the action is performed with anything, then paired with action there is another Intent attribute – given. In it we can specify any object: the user in the address book, coordinates on the map, phone number, etc. That is action indicates What shall I do, and given what to do.

about given we will talk more about the next lessons, and for now we will only use action. I have already listed some of the system action constants above, but we can use our own action as well.

As you can see from the code, I came up with and used the following actions:
ru.startandroid.intent.action.showtime
ru.startandroid.intent.action.showdate

The first will mean that I want to call an Activity that will show me the current time. Second – Activity with a date.

The following should be clearly understood: action – it’s just text. And I could just as well come up with action abcdefg123456. but the text showtime – reflects what I want to do, it is clearer and clearer. And the prefix ru.startandroid.intent.action I use to avoid collisions. The system may have an application that already uses action showtime – I don’t want to intersect with him. So my action is ru.startandroid.intent.action.showtime.

So, we created Intent with action and launched it to look for Activity. For Activity to come up, you need it Intent Filter contained an attribute action with the same valuethat and action in Intent. So we have two to create Activity, Customize them Intent Filter and display time and date.

Activity is created as usual – we create a class ActivityTime with superclass android.app.Activity and list it in the manifest as Activity. After being registered in the manifest, it will be necessary to create Intent Filter there as well. For this purpose we allocate ActivityTime, press Add, we choose Intent Filter and click OK.

Then in Intent Filter similarly create Action and in the field Name prescribed ru.startandroid.intent.action.showtime

also in Intent Filter must be created Category and in the field name select from the list android.intent.category.DEFAULT. We will not yet understand in detail why it is needed. But without that the challenge startActivity (Intent) will not find Activity.

Let’s create a layout for the new Activity, call it time.xml:



    
    

We write code in ActivityTime.java:

package ru.startandroid.develop.p0261intentfilter;

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

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

public class ActivityTime extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.time);
    
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    String time = sdf.format(new Date(System.currentTimeMillis()));
    
    TextView tvTime = (TextView) findViewById(R.id.tvTime);
    tvTime.setText(time);
  }
}

Everything is simple here – we calculate the current time and display it in TextView.

We will save everything and launch the application.

Click the Show time button:

time is displayed. That is Intent with action = ru.startandroid.intent.action.showtime found and reflected Activity, which action is also equal ru.startandroid.intent.action.showtime in Intent Filter.

Let’s go back (button Back) And now press the button Show date. The application will throw an error because it could not find an Activity that would match Intent with action = ru.startandroid.intent.action.showdate (We only created for showtime).

Let’s create this Activity, let’s call it Activity ActivityDate. The actions are the same as when creating ActivityTime:
– creating a class
– create an Activity in the manifest and create an Intent Filter for it (c action = ru.startandroid.intent.action.showdate and category = android.intent.category.DEFAULT)

Let’s call the layout file date.xml:



    
    

code ActivityDate.java:

package ru.startandroid.develop.p0261intentfilter;

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

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

public class ActivityDate extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.date);
    
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
    String date = sdf.format(new Date(System.currentTimeMillis()));
    
    TextView tvDate = (TextView) findViewById(R.id.tvDate);
    tvDate.setText(date);
  }
}

We save everything, launch the application, click Show date and we see the date. This means that Intent with action = ru.startandroid.intent.action.showdate found and reflected ActivityDate suitable for him by Intent Filter.

To pin the topic, let’s do some more. If you remember in lesson # 22, where I talked about Intent Filter, I said that one Intent can find sprat relevant Activity. In this case, the user is provided choiceWhich Activity to use. Let us provoke this to ourselves. We will do another Activity that will respond to Intent with action = ru.startandroid.intent.action.showdate. And will display the current date similar to ActivityDate. But the format of the date display will be slightly different.

Let’s create this Activity, let’s call it Activity ActivityDateEx. The actions are the same as when creating ActivityDate:
– creating a class
– create Activity in manifest and create Intent Filter for it (with action = ru.startandroid.intent.action.showdate and category = android.intent.category.DEFAULT)

We will not create a new layout file, we use an existing one date.xml. Basically, we could use all three Activities in one layout because they are exactly the same – one TextView.

code ActivityDateEx.java:

package ru.startandroid.develop.p0261intentfilter;

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

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

public class ActivityDateEx extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.date);
    
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
    String date = sdf.format(new Date(System.currentTimeMillis()));
    
    TextView tvDate = (TextView) findViewById(R.id.tvDate);
    tvDate.setText(date);
  }
}

As you can see, the difference from ActivityDate is only in the date format.

Let’s save everything and run. Click Show date and see the following selection:

Intent found two Activity, But showed for each of them the parent app name and package. In our case, both Activity are from our application, so the text is the same and you will not understand which one is which one. Let’s fix this by spelling out normal names.

Click Back to close the selection dialog. we go to manifesto and for Activity prescribe label:

Date basic for ActivityDate
Date extended for ActivityDateEx

Save and run. Click Show date and see the following selection:

So much better. Click Date extended and see the date in expanded format on ActivityDateEx.

So, we created and sent Intent with action. this Intent found Activity with the appropriate Intent Filter and displayed it. if found sprat – gave choice. The examples show the mechanism perfectly.

If you are confused as to what and where to create, I bring the project screen and the contents of the manifest.

Manifesto content (AndroidManifest.xml tab):



    
    
        
            
                
                
            
        
        
            
                
                
            
        
        
            
                
                
            
        
        
            
                
                
            
        
    

In the next lesson:

– read action from Intent




Discuss in the forum [86 replies]

Leave a Comment