Android Intent Tutorial

Android Intent Tutorial

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:

  • Start the service
  • Launch an activity
  • Display a web page
  • Display a list of contacts
  • Broadcast a message
  • 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.javatpoint.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);

To get the full code of explicit intent, visit the next page.


Android Implicit Intent Example

Let’s see the simple example of implicit intent that displays a web page.

activity_main.xml

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <EditText
  7.         android:id=“@+id/editText1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentTop=“true”
  11.         android:layout_centerHorizontal=“true”
  12.         android:layout_marginTop=“44dp”
  13.         android:ems=“10” />
  14.     <Button
  15.         android:id=“@+id/button1”
  16.         android:layout_width=“wrap_content”
  17.         android:layout_height=“wrap_content”
  18.         android:layout_below=“@+id/editText1”
  19.         android:layout_centerHorizontal=“true”
  20.         android:layout_marginTop=“54dp”
  21.         android:text=“Visit” />
  22. </RelativeLayout>

Activity class

File: MainActivity.java
  1. package org.sssit.implicitintent;
  2. import android.net.Uri;
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. public class MainActivity extends Activity {
  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.         final EditText editText1=(EditText)findViewById(R.id.editText1);
  16.         Button button1=(Button)findViewById(R.id.button1);
  17.         button1.setOnClickListener(new OnClickListener() {
  18.             @Override
  19.             public void onClick(View arg0) {
  20.                 String url=editText1.getText().toString();
  21.                 Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(url));
  22.                 startActivity(intent);
  23.             }
  24.         });
  25.     }
  26. }


Output:

android implicit intent example output 1 android implicit intent example output 2 android implicit intent example output 3

Source : https://www.javatpoint.com/android-intent-tutorial