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
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 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.
To start a SecondActivity
from a MainActivity
:
val intent = Intent(this, SecondActivity::class.java) startActivity(intent)
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.
val url = "https://www.example.com" val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) startActivity(intent)
val dial = "tel:123456789" val intent = Intent(Intent.ACTION_DIAL, Uri.parse(dial)) startActivity(intent)
val filePath = "/path/to/photo.jpg" val intent = Intent(Intent.ACTION_VIEW).apply { setDataAndType(Uri.parse(filePath), "image/*") } startActivity(intent)
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.
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:
Android Intent examples with code:
Intent intent = new Intent(this, TargetActivity.class); intent.putExtra("key", "Data to pass"); startActivity(intent);
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.
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.
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.
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);
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.
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.