You are on page 1of 4

Android Intent 

Android Intent is the message that is passed between components such as


activities, content providers, broadcast receivers, services etc. It is generally
used with startActivity() method to invoke activity, broadcast receivers etc.
The dictionary meaning of intent is intention or purpose. So, it can be
described as the intention to do action.

The LabeledIntent is the subclass of android.content.Intent class.

Android intents are mainly used to:

o Start the service


o Launch an activity
o Display a web page
o Display a list of contacts
o Broadcast a message
o Dial a phone call etc.

Types of Android Intents


There are two types of intents in android: implicit and explicit.

1) Implicit Intent
Implicit Intent doesn't specifiy the component. In such case, intent provides
information of available components provided by the system that is to be
invoked.

For example, you may write the following code to view the webpage.

1. Intent intent=new Intent(Intent.ACTION_VIEW);  
2. intent.setData(Uri.parse("http://www.google.com"));  
3. startActivity(intent);  

2) Explicit Intent
Explicit Intent specifies the component. In such case, intent provides the
external class to be invoked.

1. Intent i = new Intent(getApplicationContext(), ActivityTwo.class);  
2. startActivity(i);  
Android Implicit Intent Example
File: activity_main.xml

1. <?xml version="1.0" encoding="utf-8"?>  
2. <android.support.constraint.ConstraintLayout xmlns:android="http://
schemas.android.com/apk/res/android"  
3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
4.     xmlns:tools="http://schemas.android.com/tools"  
5.     android:layout_width="match_parent"  
6.     android:layout_height="match_parent"  
7.     tools:context="example.javatpoint.com.implicitintent.MainActivity">
8.   
9.     <EditText  
10.         android:id="@+id/editText"  
11.         android:layout_width="wrap_content"  
12.         android:layout_height="wrap_content"  
13.         android:layout_marginEnd="8dp"  
14.         android:layout_marginStart="8dp"  
15.         android:layout_marginTop="60dp"  
16.         android:ems="10"  
17.         app:layout_constraintEnd_toEndOf="parent"  
18.         app:layout_constraintHorizontal_bias="0.575"  
19.         app:layout_constraintStart_toStartOf="parent"  
20.         app:layout_constraintTop_toTopOf="parent" />  
21.   
22.     <Button  
23.         android:id="@+id/button"  
24.         android:layout_width="wrap_content"  
25.         android:layout_height="wrap_content"  
26.         android:layout_marginRight="8dp"  
27.         android:layout_marginLeft="156dp"  
28.         android:layout_marginTop="172dp"  
29.         android:text="Visit"  
30.         app:layout_constraintEnd_toEndOf="parent"  
31.         app:layout_constraintHorizontal_bias="0.0"  
32.         app:layout_constraintStart_toStartOf="parent"  
33.         app:layout_constraintTop_toBottomOf="@+id/editText" />  
34. </android.support.constraint.ConstraintLayout>  

File: MainActivity.java

1. package example.javatpoint.com.implicitintent;  
2. import android.content.Intent;  
3. import android.net.Uri;  
4. import android.support.v7.app.AppCompatActivity;  
5. import android.os.Bundle;  
6. import android.view.View;  
7. import android.widget.Button;  
8. import android.widget.EditText;  
9. public class MainActivity extends AppCompatActivity {  
10.     Button button;  
11.     EditText editText;  
12.    @Override  
13.     protected void onCreate(Bundle savedInstanceState) {  
14.         super.onCreate(savedInstanceState);  
15.         setContentView(R.layout.activity_main);  
16.   
17.         button = findViewById(R.id.button);  
18.         editText =  findViewById(R.id.editText);  
19.         button.setOnClickListener(new View.OnClickListener() {  
20.             @Override  
21.             public void onClick(View view) {  
22.                 String url=editText.getText().toString();  
23.                 Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));  
24.                 startActivity(intent);  
25.             }  
26.         });  
27.     }  
28. }  
Output

You might also like