[go: up one dir, main page]

0% found this document useful (0 votes)
22 views9 pages

35 MobileComputing Prac3

sdgsgsg

Uploaded by

prasad Gade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views9 pages

35 MobileComputing Prac3

sdgsgsg

Uploaded by

prasad Gade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Name of Student : Devendra Suresh Mourya

Roll Number : 35 LAB Assignment Number: 3

Title of LAB Assignment: Android program based on intents.

DOP : 06-09-2024 DOS : 13-09-2024

CO Mapped: CO2 PO Mapped: PO2, Signature:


PO3, PSO1
Name : Devendra Suresh Mourya SYMCA/A Roll No : 35

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

There are two main 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

An Intent object is made up of several components:


Name : Devendra Suresh Mourya SYMCA/A Roll No : 35

 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).

Commonly Used Intent Actions

 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.

 Broadcast Receivers listen for broadcasted intents and react to them.


 To declare a broadcast receiver, use the <receiver> tag in the manifest.
Intent intent = new Intent("com.example.CUSTOM_INTENT");
sendBroadcast(intent);

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">

<!-- Enter Link EditText -->


<EditText
android:id="@+id/etLink"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Link*"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>

<!-- Open Website Link Button -->


<Button
android:id="@+id/btnOpenWebsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OPEN WEBSITE LINK"
android:layout_below="@id/etLink"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textColor="@android:color/white"/>

<!-- Enter Location EditText -->


<EditText
android:id="@+id/etLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Location*"
android:layout_below="@id/btnOpenWebsite"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>

<!-- Open Google Map Button -->


<Button
android:id="@+id/btnOpenMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OPEN GOOGLE MAP"
android:layout_below="@id/etLocation"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textColor="@android:color/white"/>

<!-- Enter Message EditText -->


Name : Devendra Suresh Mourya SYMCA/A Roll No : 35

<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"/>

<!-- Share Text Button -->


<Button
android:id="@+id/btnShareText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHARE TEXT"
android:layout_below="@id/etMessage"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textColor="@android:color/white"/>

</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;

public class MainActivity extends AppCompatActivity {

private EditText etLink, etLocation, etMessage;


private Button btnOpenWebsite, btnOpenMap, btnShareText;

@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

// Open Website Link


btnOpenWebsite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = etLink.getText().toString().trim();
if (!url.isEmpty()) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "Please enter a link",
Toast.LENGTH_SHORT).show();
}
}
});

// Open Google Map


btnOpenMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String location = etLocation.getText().toString().trim();
if (!location.isEmpty()) {
Uri gmmIntentUri = Uri.parse("geo:0,0?q=" + location);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
} else {
Toast.makeText(MainActivity.this, "Please enter a location",
Toast.LENGTH_SHORT).show();
}
}
});

// 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.

You might also like