Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

Android Implicit and Explicit Intents with Examples

In Android, an Intent is a messaging object you can use to request an action from another app component. There are two types of intents in Android:

  • Explicit Intents
  • Implicit Intents

1. Explicit Intents:

Explicit intents specify the component to start by name (i.e., the fully-qualified class name). You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start.

Example of an Explicit Intent:

To start a SecondActivity from a MainActivity:

val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)

2. Implicit Intents:

Implicit intents do not name a specific component. Instead, they declare a general action to perform, allowing a component from another app to handle them. The system then determines the best component to respond to the intent or asks the user to choose an app, in the case that multiple apps could handle the intent.

Examples of Implicit Intents:

  • Open a URL in a browser:
val url = "https://www.example.com"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
  • Dial a phone number (Note: This doesn't initiate a call, but opens the dialer):
val dial = "tel:123456789"
val intent = Intent(Intent.ACTION_DIAL, Uri.parse(dial))
startActivity(intent)
  • Open a photo for viewing:
val filePath = "/path/to/photo.jpg"
val intent = Intent(Intent.ACTION_VIEW).apply {
    setDataAndType(Uri.parse(filePath), "image/*")
}
startActivity(intent)
  • Send an email:
val intent = Intent(Intent.ACTION_SEND).apply {
    type = "text/plain"
    putExtra(Intent.EXTRA_EMAIL, arrayOf("recipient@example.com"))
    putExtra(Intent.EXTRA_SUBJECT, "Email Subject")
    putExtra(Intent.EXTRA_TEXT, "Email Body Text")
}

startActivity(Intent.createChooser(intent, "Send Email"))

For implicit intents to work, there should be apps installed on the device that can handle the desired action. If no apps can handle the intent, your app may crash when trying to start the activity. To prevent this, you can first check if there's an app that can handle your intent using intent.resolveActivity(getPackageManager()).

In summary, while explicit intents are used to activate specific components, implicit intents are more flexible and can activate any app's component that can handle the requested action. Always ensure that there's a receiving component that can handle your implicit intent to avoid potential crashes.

  1. Implicit vs Explicit Intents in Android:

    • Explicit Intent:

      Intent explicitIntent = new Intent(this, TargetActivity.class);
      startActivity(explicitIntent);
      
    • Implicit Intent (e.g., opening a website):

      Intent implicitIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
      startActivity(implicitIntent);
      
    • Key Difference:

      • Explicit Intents target a specific component (activity, service, or broadcast receiver) within your app.
      • Implicit Intents don't specify the target component and are used for system-wide actions.
  2. Android Intent examples with code:

    Intent intent = new Intent(this, TargetActivity.class);
    intent.putExtra("key", "Data to pass");
    startActivity(intent);
    
  3. How to use Implicit Intents in Android:

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

    Implicit Intents are often used for actions like opening a website, sending an email, etc., where the target component isn't explicitly specified.

  4. Android Intent filter example:

    Define an intent filter in the manifest for the target component:

    <activity android:name=".TargetActivity">
        <intent-filter>
            <action android:name="com.example.ACTION_CUSTOM"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
    

    Use an implicit intent with the specified action to target the activity.

  5. Explicit Intent in Android with code samples:

    Intent explicitIntent = new Intent(this, TargetActivity.class);
    explicitIntent.putExtra("key", "Data to pass");
    startActivity(explicitIntent);
    

    Explicit Intents explicitly specify the target component (activity, service, or broadcast receiver) within your app.

  6. Android Intent.putExtra example:

    Use putExtra to include additional data in the intent:

    Intent intent = new Intent(this, TargetActivity.class);
    intent.putExtra("key", "Data to pass");
    startActivity(intent);
    
  7. Android Intent action examples:

    // Example: Send an email
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setData(Uri.parse("mailto:recipient@example.com"));
    
    // Example: Dial a phone number
    Intent dialIntent = new Intent(Intent.ACTION_DIAL);
    dialIntent.setData(Uri.parse("tel:" + phoneNumber));
    

    Actions define the operation to perform, such as sending an email or dialing a phone number.

  8. Android Intent category examples:

    Intent intent = new Intent(this, TargetActivity.class);
    intent.addCategory("com.example.CUSTOM_CATEGORY");
    startActivity(intent);
    

    Categories provide additional information about the kind of component that can handle the intent.