Intents are asynchronous messages which allow application components to request functionality from other Android components. Intents allow you to interact with components from the same applications as well as with components contributed by other applications. For example, an activity can start an external activity for taking a picture.
Intents are objects of the android.content.Intent
type. Your code can send them to the Android system defining the components you are targeting. For example, via the startActivity()
method you can define that the intent should be used to start an activity.
An intent can contain data via a Bundle
. This data can be used by the receiving component.
Android supports explicit and implicit intents.
An application can define the target component directly in the intent (explicit intent) or ask the Android system to evaluate registered components based on the intent data (implicit intents).
Explicit intents explicitly define the component which should be called by the Android system, by using the Java class as identifier.
The following shows how to create an explicit intent and send it to the Android system. If the class specified in the intent represents an activity, the Android system starts it.
1 2 3 |
Intent in = new Intent(ActivityOne.this, ActivityTwo.class); in.putExtra("Value1", "This value one for ActivityTwo"); in.putExtra("Value2", "This value two ActivityTwo"); |
Explicit intents are typically used within on application as the classes in an application are controlled by the application developer.
Implicit intents specify the action which should be performed and optionally data which provides content for the action.
For example, the following tells the Android system to view a webpage. All installed web browsers should be registered to the corresponding intent data via an intent filter.
1 2 |
Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse("https://androhub.com")); startActivity(in); |
If an 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.
If only one component is found, Android starts this component directly. If several components are identified by the Android system, the user will get a selection dialog and can decide which component should be used for the intent.
Let’s start with an example of both kind of intents.
1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java)
2. For Second Activity we are creating a separate activity. Create a new class in your package and name it as Second_Activity.java
3. Open your your AndroidManifest.xml file and make your “MainActivity” as Launcher activity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.intentdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <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="com.intentdemo.Second_Activity" android:label="@string/second_ac" > </activity> </application> </manifest> |
4. Create a layout file for MainActivity.java under res ⇒ layout folder. I named the layout file as activity_main.xml. This will be your first launching activity layout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dp" > <EditText android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="text" android:padding="15dp" > </EditText> <Button android:id="@+id/second_activity" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/next_activity" /> <EditText android:id="@+id/url" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textUri" android:padding="15dp" /> <Button android:id="@+id/go_url" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/open_url" /> <EditText android:id="@+id/phone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="number" android:padding="15dp" /> <Button android:id="@+id/call_number" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/call" /> </LinearLayout> |
5. Add the following code in MainActivity.java activity. In this following code we use both Implicit and Explicit Intents and also learn how to pass values from one activity to another.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
package com.intentdemo; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends ActionBarActivity implements OnClickListener { Button nextactivity, show_url, call; EditText simple_text, url_text, number_text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nextactivity = (Button) findViewById(R.id.second_activity); show_url = (Button) findViewById(R.id.go_url); call = (Button) findViewById(R.id.call_number); simple_text = (EditText) findViewById(R.id.text); url_text = (EditText) findViewById(R.id.url); number_text = (EditText) findViewById(R.id.phone); // Implementing Click Listeners to buttons nextactivity.setOnClickListener(this); show_url.setOnClickListener(this); call.setOnClickListener(this); } @Override public void onClick(View v) { int id = v.getId(); // Getting ids of buttons switch (id) { // Explicit Intent case R.id.second_activity: // Starting second activity via Intent Intent in = new Intent(MainActivity.this, Second_Activity.class); in.putExtra("value", simple_text.getText().toString()); // Passing // values // from one // activity // to // another startActivity(in); break; // Implicit Intent case R.id.go_url: // Opening url into browser Intent url = new Intent(Intent.ACTION_VIEW, Uri.parse(url_text .getText().toString())); startActivity(url); break; case R.id.call_number: // Calling entered number Intent call = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:" + number_text.getText().toString())); startActivity(call); break; } } @Override protected void onResume() { // Whenever activity resumes all the edittexts become empty simple_text.setText(""); url_text.setText(""); number_text.setText(""); super.onResume(); } } |
6. Create a layout file for second activity under res ⇒ layout folder. I named the layout file as second_activity.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?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:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" /> </LinearLayout> |
7. Add the following code in Second_Activity.java activity. In this following code we learn how to get the intent values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.intentdemo; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.widget.TextView; public class Second_Activity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.second_activity); // Getting value via intent and storing in string String value = getIntent().getStringExtra("value"); TextView text = (TextView) findViewById(R.id.textView1); // Setting stored intent value at text text.setText(value); } } |
8. Run the application and you will get the output as shown in video.
Thanks 🙂
Subscribe to us and get the latest news.