![]() |
VOOZH | about |
The intent is a messaging object which tells what kind of action to be performed. The intentβs most significant use is the launching of the activity. Intent facilitates the communication between the components.
Note: App components are the basic building blocks of App.
Starting Activity
An activity represents the single screen in an app, Bypassing intent instance we can start an activity.
Example:
You can add extra information by using putExtra().
Starting a Service
A Service is a component that performs operations in the background without a user interface, which is also called a background process.
Delivering a Broadcast
A broadcast is a message that any app can receive. In android, the system delivers various broadcast system events like device starts charging, disable or enable airplane mode, etc.
There are two types of intent
For example: when you tap the share button in any app you can see the Gmail, Bluetooth, and other sharing app options.
Example:
Most of the intent filter are describe by its
Syntax:
Adds an action to an intent filter. An <intent-filter> element must contain one or more <action> elements. If there are no <action> elements in an intent filter, the filter doesn't accept any Intent objects.
Examples of common action:
Syntax:
Adds a category name to an intent filter. A string containing additional information about the kind of component that should handle the intent.
Example of common categories:
Syntax:
Adds a data specification to an intent filter. The specification can be just a data type, just a URI, or both a data type and a URI.
Note: Uniform Resource Identifier (URI) is a string of characters used to identify a resource. A URI identifies a resource either by location, or a name, or both.
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Add dependencies to the build.gradle(Module:app) file
Add the following dependency to the build.gradle(Module:app) file. We are adding these two dependencies because to avoid using findViewById() in our MainActivity.kt file. Try this out otherwise use the normal way like findViewById().
π Imageapply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 4: Working with the AndroidManifest.xml File
Following is the code for the AndroidManifest.xml File.
Step 5: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Output with Explanation:
π ImageClick on Send Button, you will see a screen like this,
π ImageNow choose Gmail app,
π ImageNow go to our app and click the view button,
π Imageour app dummy app. You can any app from these options because we are using a view intent filter.