Intents are abstract description of operation to be performed. Using intents we can call another activity of the same application or other application. For example, an activity can start an external activity for taking a picture. Intents are objects of the android.content.Intent type. Now it sends them to the Android Intent Resolver defining the components you are targeting.
Intent can contain data, which can be used by receiver.
Intent can be useful in reuse of other application component. For example, from an activity of an application, it can trigger another component in the Android system, which manages photos, even if this component is not part of the application. In this, the selected photo will be returned to the application.
Below is the step by step understanding of intent.
To start an activity, startActivity(intent) method is used. The parameter is the object of intent. The syntax is as below.
Intent i = new Intent(this, NextActivity.class); startActivity(i);
Android gives flexibility to share data through intents to both explicitly and implicitly.
Explicit intents explicitly define the component which should be called by the Android system, by using the Java class as identifier. Explicit intents are typically used within an application as the classes in an application are controlled by the application developer. The following shows how to create an explicit intent and send it to the Android system to start an activity.
Intent i = new Intent(this, ActivityTwo.class); i.putExtra("Value1", "This value one for ActivityTwo "); i.putExtra("Value2", "This value two ActivityTwo");
Implicit intents specify the action which should be performed and optionally data which provides content for the action. Implicit intent is sent to the Android system, it searches for all components which are registered for the specific action and the fitting data type. For example, the following tells the Android system to view a webpage.
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); startActivity(i);
Once you send the data, now how to read it. Now using getExtras() method we can retrieve data.
Bundle bundle = getIntent().getExtras(); String message = bundle.getString("Value1");
An intent filter specifies the type of intents that the component would like to receive.
When you create an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object. Below is the code example.
<activity android:name=”.HelloWorld” android:label=”@string/app_name” > <intent-filter > <action android:name=”android.intent.action.VIEW”/ > <category android:name=”android.intent.category.DEFAULT”/ > <category android:name=”android.intent.category.BROWSABLE”/ > <data android:scheme=”http” android:host=”androidium.org”/ > </intent-filter > </activity >
Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse(“http://google.com”)); startActivity(intent);