[go: up one dir, main page]

0% found this document useful (0 votes)
29 views22 pages

X-4 - Intent

Uploaded by

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

X-4 - Intent

Uploaded by

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

Mobile

Development

1 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


course support material is
intended to complement the
lessons and does not replace
the full content of the course.
Attendance remains mandatory.

2 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


SESSION PLAN

INTENT ?

Intent filters

Passing data
between
activities
INTENT

New activity
with results

Implicit
intents

Explicit
intents

3 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


Intent: definition
An Intent in Android is an object that allows components like activities and services to communicate with
each other, typically used to start new activities, launch services, or send broadcasts.

Android uses Intent for communicating between the components of an Application and also from one
application to another application.

Intent are the objects which is used in android for passing the information among Activities in an
Application and from one app to another.

An intent involves two subjects:


• The sender wants a job to be performed and thus publishes a message (i.e., an intent)
• The receiver is any component which is able to perform the job

4 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


Intent: definition
Intents are used in a variety of situations:
• Taking pictures from the camera
• Starting a new web search
• Sending e-mails
• Sending SMS’s

All these features take advantage of the most significant, use of Intents, i.e., launching new Activities

There exist only two ways of launching a new Activity:


• By name: you know the exact name of the Activity you want to launch
• By action: you want something to be done for you (e.g., taking a picture)

Consequently, Android provides two kinds of Intents:


• Explicit intents, which specify the exact Activity to which the Intent should be given .
• Implicit Intents, which don't declare the name of the Activity to start, but instead declare an action to perform .

5 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


Intent: Explicit intents

• Explicit Intents are typically used for launching new Activities within the same application
• Nothing in the Intent object other than the name of the Activity to be launched matters
All other fields (action, data, category) are null

6 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


Intent: Implicit intents

Each implicit Intent contains information useful to the receiver for performing the job:
• Action: the job that the sender can require to perform
• Data: the data on which the job will run
• Category: the execution requirements (e.g., of being executed in a browser)

7 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


Intent: Implicit intents
Each implicit Intent contains information useful to the receiver for performing the job:
• Action: the job that the sender can require to perform
• Data: the data on which the job will run
• Category: the execution requirements (e.g., of being executed in a browser)

▪ The system determines which Activity is best to run based on the fields (e.g., action) of the implicit Intent.
▪ If more than one Activity can handle the Intent, a dialog for the user to select which app to use is displayed.

8 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


Intent: Launching an activity

- To start an activity, call the startActivity (Intent) method.


- Once the startActivity (Intent) method is invoked
• in case of implicit intent, the resolution mechanism identifies the Activity that best matches the intent fields
• In case of explicit intent, the system receives the call and starts an instance of the Activity specified by the Intent

- It is possible to pass some parameters to the start Activity by adding them as extra content of the intent.
- Within the just-started Activity, you can inspect the received Intent by using the getIntent() method

9 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


Intent: Receiving implicit intents

▪ How to inform the system that an Activity can handle a specific type of implicit Intent.
▪ This can be done by advertising that a given Activity can manage a specific implicit Intent, i.e., by
associating the Activity with an Intent filter .

▪ An Intent filter:
• declares the capability of an Activity of receiving implicit Intents of a specific type.
• delimits the Intents an Activity can handle.

10 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


Intent: Request result from an activity

▪ You can start another Activity and receive a result back.


▪ Examples:
▪ Start the camera and receive the photo as a result.
▪ Download a using the new activity and sends it to the previous activity.
▪ You can use StartActivityForResult(intent, UR_REQUEST_CODE) (or onActivityResult()) method to launch an activity and get
the result.

11 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


Intent: Request result from an activity

▪ As soon as the user is done with the called Activity, the result will be sent back to the first Activity.
▪ Along with the actual result, additional information is returned from the called Activity to the calling Activity!
▪ The request code originally sent by the calling Activity.
▪ A result code to notify that the operation in the called Activity has been successful, (you can Imagin it as HTTP status codes ).
▪ When the second Activity returns, the system invokes the onActivityResult() method on the calling Activity.
▪ The data are sent back to the calling Activity as an intent.

12 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


Intent: Request result from an activity

▪ The calling Activity uses different request codes to distinguish between different types of requests.
• When receiving a result, the calling Activity uses the request code to identify the provenance of the result.

▪ The request codes only makes sense for the calling Activity, while they do not have any particular meaning for the
called Activity.

13 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


Intent: Returning data from an activity

▪ In case the called Activity is part of our application, we have to deal also with the problem of returning data to the
calling Activity.

▪ We need to:
• Perform the operation for which we launched the second Activity
• Pack the result into an Intent object
• Determine the result code
• Terminate the called Activity

14 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


Intent: Returning data from an activity
▪ An Activity represents a task the user can achieve
▪ To this end, it may be possible for the calling Activity to provide some parameters to the second Activity
➢ These parameters are sent within the Intent as extra’s
▪ We already know how to inspect the triggering Intent within the second Activity.
▪ The called Activity produces a result of some kind .
▪ These data can be packed into an Intent by using the Intent's putExtra() method.
▪ The operation performed in the called Activity may succeed or fail, and the resulting data may change accordingly.
▪ You can set the result and the corresponding result code.

15 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


Intent: Returning data from an activity
▪ The Intent containing the result will not be fired until the called Activity is running .
• It is the Operating System (OS) that passes Intents between different components.
▪ Hence, when the user is done with the Activity, it has to be terminated
▪ Usually, it is up to the Operating System to decide when to terminate an Activity
• However, is some cases the user performs an operation of the Ul that indicates that he is done with the current
Activity .
▪ To manually terminate an Activity we can use the finish () method .

16 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


example
Let's create an activity that adds new alarms in the main system alarm app
We use implicit intent

17 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


START YOUR MACHINE

18 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


HANDS-ON
LAB – 4 – PART 1
Create an application with a main activity that contains four buttons, each triggering different functionalities.

• Button 1: Open URL in Browser


Create a new activity that contains an input field where the user can enter a URL.
When the user clicks the "Submit" button, the application should open the entered URL in the device's default web browser.
Hints:
Use an implicit intent to open a URL. Set the action to Intent.ACTION_VIEW and provide the URL as a data URI. Ensure that the URL is formatted
correctly (including the protocol, e.g., "http://") (intent.setData(Uri.parse("http://www.example.com"));).

• Button 2: Open Location in Map


Create a new activity that allows the user to enter a location (e.g., "Eiffel Tower").
When the user clicks the button, the system's map application should open, showing the specified location.
Hints:
Use an implicit intent with a geo: URI to launch the maps application. Construct the URI by encoding the location string, and set the action to
Intent.ACTION_VIEW. This will prompt the device to find a suitable mapping application.
intent.setData(Uri.parse("geo:0,0?q=" + Uri.encode("Eiffel Tower")));

19 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


HANDS-ON
LAB – 4 – PART 1
• Button 3: Capture Image Using Camera
Open a new activity with a button that, when pressed, launches the camera.
After the user captures an image, display the image in the activity.
Hints:
Use an explicit intent with the action MediaStore.ACTION_IMAGE_CAPTURE to launch the camera. Make sure to handle the result properly
to retrieve the image and display it in the activity.

Button 4: Calculate Sum of Two Numbers


Create a new activity that contains two input fields for the user to enter numbers.
When the user clicks "Submit," display the sum in a new activity.
Hints:
Use an explicit intent to navigate to the new activity. Pass the two numbers as extras in the intent using key-value pairs. In the receiving
activity, retrieve these values to perform the addition and display the result.

20 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


HANDS-ON
LAB – 4 – Part 2: SMS Application
Create a second application that can send SMS messages through intents.

• Button to Send SMS

✓ In the first application, add a new button that opens a new activity containing two input fields: one for the phone number and one for
the message body.
✓ When the user clicks "Submit," the second application should open, pre-filled with the phone number and message body.

Hints:

Create an implicit intent with the action Intent.ACTION_SENDTO. Set the data to a smsto: URI that includes the phone number
(intent.setData(Uri.parse("smsto:" + phoneNumber)) . You can also add the message body as an extra in the intent. This will allow the
system to launch an SMS application with the pre-filled information.

Ensure that you have the necessary permissions in your AndroidManifest.xml file. Add the following lines to request camera and SMS permissions:
App 1 : <uses-permission android:name="android.permission.CAMERA"/>
App 2: <uses-permission android:name="android.permission.SEND_SMS"/>

21 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com


HANDS-ON
LAB – 4 –

Run Your Project: After finishing your project, launch it on your emulator or mobile device.

Record Your Screen: Create a screen recording of your app in action. Please keep the recording to a maximum length of 60 seconds.

Submit Your Work: Fill out the form linked below with your information and upload the recorded video.

Important: Ensure your name appears somewhere within the application.

https://forms.gle/Kfw6GispBDH5NugW9

22 Pr. MOUNCIF Hamza, hamza.Mouncif.4@gmail.com

You might also like