What are Intents

Description

An intent is an action with its associated data.

Android uses intents to invoke components. The components in Android includes

  • activities (UI components),
  • services (background code),
  • broadcast receivers, and
  • content providers.

You can use intents to invoke external applications or internal components.

You can use intents to raise events so that others can respond in a manner similar to a publish-and-subscribe model.

You can use intents to raise alarms.

Example

The following code shows how to use Intent to open an Activity.

Imagine you've the following activity:


public class BasicViewActivity extends Activity
{//from w w  w . j  a  v a  2s  .co m
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        ...
    }
}

Then you register this activity in the manifest file, making it available for other applications to invoke.


<activity android:name=".BasicViewActivity"
          android:label="Basic View Tests">
 <intent-filter>//from  w w  w  . j av  a2 s.  c  o m
   <action android:name="com.java2s.intent.action.ShowBasicView"/>
   <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>
</activity>

To use an intent to invoke this BasicViewActivity:


public static void invokeMyApplication(Activity parentActivity)
{/*from w w  w .ja v  a 2  s . c  om*/
   String actionName= "com.java2s.intent.action.ShowBasicView";
   Intent intent = new Intent(actionName);
   parentActivity.startActivity(intent);
}

Note

The general convention for an action name is


<your-package-name>.intent.action.YOUR_ACTION_NAME

BasicViewActivity can get the intent that invoked it.


class BasicViewActivity extends Activity
{/*  w w  w  .j a va  2 s .  c  om*/
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        ...
        ...
        Intent intent = this.getIntent();
        if (intent == null)
        {
            Log.d("test tag", "This activity is invoked without an intent");
        }
    }
}




















Home »
  Android »
    Android Basics »




Hello Android
Resources
Activity
Calendar
Camera
Contact
Content Provider
Database
Hardware
Intent
Location
Media
Network
Notification
Preference
Sensor
Service
SMS