35 MobileComputing Prac3
35 MobileComputing Prac3
Practical No. 3
Aim: Android program based on intents.
Description:
Intents
Intents in Android are a fundamental concept used to facilitate communication between components
within an Android application or between different applications. They are a powerful tool that allows
developers to implement various types of actions, from launching new activities within the app to
enabling interactions between apps. Here’s a detailed explanation:
What is an Intent?
An Intent is a messaging object you can use to request an action from another app component. The
primary purposes of intents are:
1. Starting an Activity: You can use an intent to start a new activity within your app or another app.
This is the most common use of intents.
2. Starting a Service: Intents can be used to start or bind to a background service.
3. Delivering a Broadcast: Intents can be used to send a broadcast message to multiple apps.
Types of Intents
1. Explicit Intent:
An explicit intent specifies the component to start by its name (the fully qualified class name).
Explicit intents are typically used to start a specific component within your app, such as
launching a specific activity or service.
Example: Navigating from one activity to another within the same app.
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
startActivity(intent);
2. Implicit Intent:
An implicit intent does not name a specific component. Instead, it declares a general action to
perform, which allows any app installed on the device that can handle that action to respond.
The system resolves the intent based on the available components and their intent filters.
Example: Opening a web page, sending an email, or sharing a piece of text.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.example.com"));
startActivity(intent);
Intent Structure
Action: A string that specifies the action to be performed (e.g., ACTION_VIEW, ACTION_SEND,
ACTION_MAIN).
Data: The data to operate on, typically expressed as a URI (e.g., content://contacts/people/1).
Category: Provides additional information about the action to be performed (e.g.,
CATEGORY_LAUNCHER).
Extras: A Bundle of additional information (key-value pairs) that provides additional information
to the receiver.
Component: The name of the component to start. This is used in explicit intents.
Flags: Special flags that control how the intent is handled (e.g., FLAG_ACTIVITY_NEW_TASK).
ACTION_VIEW: Display the data to the user in the most reasonable way (e.g., open a browser to
view a web page).
ACTION_SEND: Send data to another activity (e.g., share text or images).
ACTION_MAIN: Start up the main entry point of the app.
ACTION_DIAL: Open the dialer with a given number pre-filled.
ACTION_EDIT: Edit the given data.
ACTION_PICK: Select an item from the data.
Intent Filters
Intent Filters are a crucial part of how the Android system resolves implicit intents. They are declared in
the manifest file of an app to tell the system what kinds of intents the app can handle.
An intent filter is used by the Android system to determine which apps or components can
respond to an intent.
It typically includes:
o Actions: The type of action the filter can respond to.
o Data: The type of data the filter can operate on.
o Categories: Additional information about the action, often optional.
Broadcast Intents
Broadcasts are messages that any app can receive. The system sends broadcasts for system events like
battery low, Wi-Fi connection changes, etc. You can also send custom broadcasts to communicate within
your app.
Pending Intent
A PendingIntent is a token that you can give to another app (e.g., notification manager, alarm manager),
which allows the receiving app to perform a predefined action on your behalf, even if your app is not
running.
Name : Devendra Suresh Mourya SYMCA/A Roll No : 35
Code:
Activity_main.xml
<RelativeLayout
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"
tools:context=".MainActivity">
<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Message*"
android:layout_below="@id/btnOpenMap"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</RelativeLayout>
MainActivity.java
package com.example.prac3;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etLink = findViewById(R.id.etLink);
etLocation = findViewById(R.id.etLocation);
etMessage = findViewById(R.id.etMessage);
btnOpenWebsite = findViewById(R.id.btnOpenWebsite);
btnOpenMap = findViewById(R.id.btnOpenMap);
btnShareText = findViewById(R.id.btnShareText);
Name : Devendra Suresh Mourya SYMCA/A Roll No : 35
// Share Text
btnShareText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = etMessage.getText().toString().trim();
if (!message.isEmpty()) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent, "Share via"));
} else {
Toast.makeText(MainActivity.this, "Please enter a message",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Name : Devendra Suresh Mourya SYMCA/A Roll No : 35
Output:
Name : Devendra Suresh Mourya SYMCA/A Roll No : 35
Name : Devendra Suresh Mourya SYMCA/A Roll No : 35
Conclusion:
In this practical implementation, we created an Android activity that uses both explicit and implicit
intents to perform various actions such as opening a website, navigating to a location on Google Maps,
and sharing a text message. By leveraging intents, we enabled seamless interaction between different
components within the app and other apps, demonstrating the power and versatility of intents in
Android development. The user interface was designed using a combination of EditText fields for input
and Button components for triggering the intents, showcasing a straightforward and user-friendly
approach to handling common tasks.