android-logo

Implicit Intent example

Table of Content

  • Important Methods of Implicit Intent in Android
  • Implicit Intent in Android Steps

In Android development, Implicit Intent is the type of Intent that does not specify the targeted component. Instead, Implicit Intent describes the operation (and sometimes data to provide content for the operation) that is required by the app. When an Android System receives an incoming Intent, it checks the whole device for all the components that can handle the Intent. When there is only one component that is able to handle the Intent, the system simply launches that single component.

However, if the device has more than one component that can handle the Intent, then the system displays a dialog box to the user. This dialog box shows all the appropriate components for the Intent. Out of these choices, the user has to select only one to perform the required operation.


Important Methods of Implicit Intent in Android

The three most important methods for implementing Implicit Intent in Android.

  1. Context.startActivity() This method is used for either launching a new activity or making an existing activity to perform the action.
  2. Context.startService() This method is used for either starting a new service or delivering instructions to an existence service.
  3. Context.sendBroadcast() This method is used for delivering the message to broadcast receivers

Implicit Intent in Android Steps

In the next step, open the MainActivity.java class file of your project in order to add the following code in it:

MainActivity.java


package com.androidaura.intent;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    Button btnSend;
    EditText edittext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnSend = findViewById(R.id.btnSend);
        edittext =  findViewById(R.id.edittext);

        btnSend.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);
            }
        });
    }
}

After adding the code in your java class file, you have to open the layout file, i.e. activity_main.xml, in order to add the following code in it:

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter URL"
        android:ems="10"/>

    <Button
        android:id="@+id/btnSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:p="Submit" />

</LinearLayout>

If you observe the above mentioned code, we have used this file to develop our UI (User Interface) elements. For example, we have used this code to establish that our button would have the p “Submit” written on it. Other than that, we have also set the default state of our p field to have “Enter URL” in order to hint the user what kind of data the project will accept.

Output
implicit-intent-1
implicit-intent-2

previous-button
next-button

facebook-logo
twitter-logo
instagram-logo

2020 © All rights reserved.