An intent is an abstract description of an action expected to be performed. Android Intent reflects English meaning of word intent, determination to do something. It is kind of a black box containing set of instructions to perform an action like,
An Android app or the OS will be listening for an Intent. Based on the information given in an Intent the action will be performed.
At low level, Intent is a java class that belongs to content package in Android (android.conent.Intent). Among all its attributes, couple of attributes are important knowing and they are,
These attributes hold the values that are evaluated while mapping the intents with appropriate application component which may be an activity, a service or broadcast receivers.
Other attributes are
Android Intent can be broadly classified into the following two categories,
Each component of application such as activities, services and broadcast receiver has set of intent filters for each functions for which the components are responsible. These intent filters are specified in Android Manifest file. All the intents passing to the component will be filtered through these set of intent filters to capture the right component. In other case, if the component name is directly specified in the intent instantiation, then there is no need of getting through these intent filters.
Intent filters includes the action, data and many other optional attributes. The intents are mapped with the component using these attributes holed by the intent filter. Intent filters are specified in the Android manifest file using <intent-filter> tag. For example, the intent filters for main activity will be as follows.
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>.
Intent filters for a broadcast receiver can be specified to the system in two ways. It can be done by either in static or in dynamic manner. In static method, the intent filters are declared in Manifest file with <intent-filter> …</intent-filter> tags. But, in dynamic method, the intent filters are instantiated by the program code itself. The later can receive intent from other application where as the former can receive intent within application only.
This example will write a message that is broadcast in Android log.
Download Source Code: Intent Example
Create a new Android application project to broadcast a content entered by the user. The content to be broadcasted will be taken from the user through a main Android activity component. On clicking the send button, this content will be send to the broadcast receiver component. For that the activity layout contains a content edit text field and a send button.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/toAddress" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="44dp" android:layout_marginTop="26dp" android:text="Content" /> <EditText android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView3" android:layout_below="@+id/textView3" android:ems="10" android:inputType="textMultiLine" /> <Button android:id="@+id/btnSend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/content" android:layout_below="@+id/content" android:layout_marginTop="26dp" android:onClick="requestToSend" android:text="Send" /> </RelativeLayout>
On clicking the Send button the requestToSend() method will be invoked. In this method, the user input is read from the form layout and pass through an intent using sendBroadcast method. Before that, the intent has to be instantiated with the current and destination component. The entire code for the activity java class is shown below.
package com.javapapers.android.broadcastcontent; import android.os.Bundle; import android.widget.EditText; import android.app.Activity; import android.content.Intent; import android.view.View; import com.javapapers.android.broadcastcontent.R; public class MainActivity extends Activity { EditText content; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void requestToSend(View view) { content = (EditText)findViewById(R.id.content); Intent intent = new Intent(MainActivity.this, Receiver.class); intent.putExtra("content", content.getText().toString()); sendBroadcast(intent); } }
While invoking the in built sendBroadcast() method by passing intents as its argument, the onReceive callback method of broadcast Receiver component will be called. This method includes the following code.
@Override public void onReceive(Context context, Intent intent) { String content = intent.getStringExtra("content"); try{ Log.d(LOGCAT, content); } catch(Exception e) { Log.d(LOGCAT, "Problem in receiving broadcast!"); } }
It gets the value of content text field using getStringExtra method and strore it into a local variable. Using this variable the corresponding content will be written into the log finally. The complete code for the broadcast receiver component is shown below.
package com.javapapers.android.broadcastcontent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class Receiver extends BroadcastReceiver{ private static final String LOGCAT = null; @Override public void onReceive(Context context, Intent intent) { String content = intent.getStringExtra("content"); try{ Log.d(LOGCAT, content); } catch(Exception e) { Log.d(LOGCAT, "Problem in receiving broadcast!"); } } }
Before running the application the BroadcastReceiver component should be added to the android manifest file. It should be specified inside <application> tag like as other component. The code snippet for the specification of Broadcast receiver in the manifest file will be as follows.
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="Receiver" ></receiver> </application>
As we discussed above, if any component is supposed to map with the name of the component directly, then there is no need of specifying any intent filters to those component. The receiver component has only the name attribute and has no intent filters. Why because, this receivers are mapped with the name of the receiver component named Receiver. This is done inside requestToSend() function of the MainActivity class like,
Intent intent = new Intent(MainActivity.this, Receiver.class); sendBroadcast(intent);
If the intent is instantiated with the action or data like as,
Intent intent = new Intent(Intent.ACTION_SEND);
Then the receiver component should be declared with the intent filter inside manifest file. Then can only the receiver will respond to the Broadcast event triggered from other application component.
Download Source Code: Intent Example
As we can conclude that the Android intents are bundle of information needed to perform the required job; Intent filters are used to filter the unwanted intents which are not corresponding to the operations of specific component; and broadcast intents are used to pass information between two components or to communicate betwen two component.
Comments are closed for "Android Intent".
i been very confused about implicit and explicit
intents ,Thank you your tutorial helped in understanding intent concept very well
1) Implicit Intent::
Implicit Intents have not specified a component; instead, they must include enough information for the system to determine
which of the available components is best to run for that intent.
2) Explicit Intent::
Explicit Intents have specified a component (via setComponent(ComponentName) or setClass(Context, Class)),
which provides the exact class to be run. Often these will not include any other information,
simply being a way for an application to launch various internal activities it has as the user interacts with the application.
[…] Android intent service extends the GCMBaseIntentService. It has callback methods and they are called on events […]
Hi joe,
I Create on Demo for Sharing Multiple Image sharing by using Intent (ACTION_SEND)
Its working fine in Whatsapp ,F.B,etc but issue in VIBER Application There is not sending Any Image .Give Force Close.and stop the application ..
Is there any Suggestion so , Let me tell about this issue .
Thanks
Virat