Android Intent

The message passed between components such as activities, content providers, broadcast receivers, services, etc, is what the Android Intent is. To invoke an activity, broadcast receivers, etc, the startActivity() method is used with the Android Intent. The intent has a simple meaning of intention or purpose, i.e, the intention to do an action. The android.content.Intent class has a subclass LabeledIntent.

Usage of Android Intents:

The main usage of the Android intents are:

  • To start the service
  • To launch an activity
  • To display a web page
  • To display a list of contacts
  • To broadcast a message
  • To dial a phone call etc.

Types of Android Intents:

In Android, intents can be of two types, i.e, implicit and explicit.

Implicit Intent:

To provide the information of all the available components to be invoked that are provided by the system and without specifying the component the Implicit Intent is used.

Example: To view the webpage:

Intent intent=new Intent(Intent.ACTION_VIEW);  
intent.setData(Uri.parse("http://www.example.com"));  
startActivity(intent);

Explicit Intent:

To define the component, such that the external class to be invoked is provided by the intent, the Explicit Intent is used.

Example:

Intent i = new Intent(getApplicationContext(), ActivityTwo.class);  
startActivity(i);

Android Implicit Intent Example

In the below example, we are demonstrating the usage of the Android implicit intent to display a web page.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="MainActivity">
 
    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="60dp"
        android:ems="10"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.575"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_marginLeft="156dp"
        android:layout_marginTop="172dp"
            android:text="GO"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />
</android.support.constraint.ConstraintLayout>

Activity class:(File: MainActivity.java)

package com.example.radioapp;
 
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends AppCompatActivity {
 
    Button button;
    EditText editText;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        button = findViewById(R.id.button);
        editText =  findViewById(R.id.editText);
 
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url=editText.getText().toString();
                Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
            }
        });
    }
}

Output 1:

Output 2:

Output 3:

Content Protection by DMCA.com
Please Share