Access an activity using implicit intents in Android

June 23, 2012
By

In Android operating system, intents are used for inter component communication. They acts as a messenger from one component to another. An intent may carry information such as target component name, filter action name, data , filter category , extras and flags. If an intent carry target component name, then that alone is used to identify a target component. Such a component is called explicit intent. If an intent does not carry target component name, then filter action name or data or filter category may be used to identify a target component. Such an intent is called implicit intent. The process of identifying a target component from action name, data and category is called intent resolution. In this article we will create an application which will make use an implicit intent for accessing a target activity component.


1. Create an Android Project namely “ImplitIntentDemo”

New Android Project

Figure 1 : New Android Project


2. Select android build target

Android Built Target

Figure 2: Android Built Target


3. Enter application details

Application Details

Figure 3: Application Details


4. res/values/strings.xml

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

    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">ImplicitIntentDemo</string>
    <string name="btn_lbl">Open Second Activity</string>
    <string name="txt_second_activity">This is Second Activity</string>

</resources>
  • Two new strings namely “btn_lbl” and “txt_second_activity” are added
  • The string “btn_lbl” is used in the layout file res/layout/main.xml
  • The string “txt_second_activity” is used in the layout file res/layout/second.xml

5. res/layout/second.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/txt_second_activity"
    />
</LinearLayout>
  • This file acts as a layout for our second activity
  • The second activity will contain a textview

6. src/in/wptrafficanalyzer/implicitintentdemo/SecondActivity.java


package in.wptrafficanalyzer.implicitintentdemo;

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

public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
    }
}
  • This is an activity file, which extends the class android.app.Activity
  • We need to override the method onCreate(), which sets second.xml as the layout of this activity

7. AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.wptrafficanalyzer.implicitintentdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".SecondActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="in.wptrafficanalyzer.secondactivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>

  • The capabilities of the new activity “SecondActivity” is  specified in this file
  • The “name” attribute of the <activity> element specifies the component name which is used if this activity is called using an explicit intent
  • The capabilities of an activity is specified using the element <intent-filter>
  • The “name” attribute of the <action> element specifies an action name using which explicit intents can access this activity
  • The “name” attribute of the <category> element specifies a category name for this activity
  • Since we don’t need to sent any data to this activity, we are not specifying any <data> element

8. res/layout/main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_lbl" />

</LinearLayout>

  • This is the layout file for “MainAcitivity”
  • This activity will contain a button, clicking on which will open the “SecondActivity”

9. src/in/wptrafficanalyzer/implicitintentdemo/MainActivity.java


package in.wptrafficanalyzer.implicitintentdemo;

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 {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /** Defining a click event listener **/
        OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                /** Creating an implicit intent using activities action name **/
                Intent intent = new Intent("in.wptrafficanalyzer.secondactivity");

                /** Accessing the intended activity */
                startActivity(intent);

            }
        };

        /** A reference to button object of the main.xml */
        Button btn = (Button) findViewById(R.id.btn);

        /** Setting and event listener for the button click */
        btn.setOnClickListener(listener);

    }
}

  • ClickEvent listener is defined in this file to handle the button click
  • An implicit intent will be created  using action name of the intent filter

10. Application in execution

Main Activity

Figure 4 : Main Activity

Second Activity

Figure 2: Second Activity

  •  The second activity is called using implicit intent

11. Download


12. Reference

http://developer.android.com/guide/index.html

How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , ,

One Response to Access an activity using implicit intents in Android

  1. tanuja on April 15, 2015 at 5:51 pm

    hello sir I am trying to run Location Rout Direction map v2 in eclipse kepler but it is not work kindly clear my queryand give me solution

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner