Table of Contents
hide
Introduction
- In Android development, an “intent” is a fundamental concept that represents an abstract description of an operation to be performed.
Definition
- Intents are a crucial part of the Android platform’s component-based architecture, enabling different components (such as activities, services, and broadcast receivers) to interact with each other.
- Intents are a powerful mechanism for building interactive and interconnected Android applications.
Characteristics
- It is a messaging object that is used to request an action from another app component or system service.
- It is mostly used to start an activity, send broadcast receivers, start services, and send messages between two activities.
- In other words, Intents are used to request an action from another component (either within our app or from a different app) and can be used for a wide range of tasks, including starting activities, broadcasting events, and passing data between components.
- They facilitate the modular design of Android apps and allow different app components to work together seamlessly.
Types
- There are the following types of Intents in an Android system –
- Explicit Intents:
- These are used to start a specific component (e.g., an activity, service, or broadcast receiver) within from own app.
- We specify the target component by its class name or action.
- Explicit intents are used when we know exactly which component we want to launch.
-
Syntax:
- Explicit Intents:
Intent intent = new Intent(context, TargetActivity.class); startActivity(intent);
-
- Implicit Intents:
- These are used to request an action to be performed by components outside our app.
- We specify the action we want to perform, and the Android system determines the appropriate component to handle it.
- For example, We can use implicit intents to open a web page, make a phone call, or send an email.
- Syntax:
- Implicit Intents:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(“http://www.example.com”)); startActivity(intent);
-
- Passing Data Intents:
- Intents can carry data between components.
- We can attach extra data to an intent using key-value pairs, which can be useful when we need to pass information from one activity to another.
- Syntax:
- Passing Data Intents:
Intent intent = new Intent(context, TargetActivity.class); intent.putExtra(“key”, “value”); startActivity(intent);
-
- Broadcast Intents:
- These are used to send system-wide events or messages.
- We can use them to notify other components of events happening within our app or to respond to system-level events like battery status changes or network connectivity.
- Syntax:
- Broadcast Intents:
Intent intent = new Intent(“custom.action”); sendBroadcast(intent);
-
- Pending Intents:
- These are used to defer an intent until a later time or to grant permission to another app to execute an intent on our app’s behalf.
- Pending intents are often used with notifications and alarms.
- Syntax:
- Pending Intents:
Intent intent = new Intent(context, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
-
- Implicit Intent Resolution:
- When we use an implicit intent, the Android system determines which component should handle the intent based on its action and data. If multiple components can handle the intent, the system shows a dialog to let the user choose the preferred app.
- Implicit Intent Resolution:
0 Comments