Android Topics 1 What are Intents 2 Implicit

  • Slides: 16
Download presentation
Android Topics 1. What are Intents? 2. Implicit Intents vs. Explicit Intents 3. What

Android Topics 1. What are Intents? 2. Implicit Intents vs. Explicit Intents 3. What is the content of an Intent message? 4. Practice: In-Class App

What are Intents? • Most apps require us to send intents • • Example:

What are Intents? • Most apps require us to send intents • • Example: the Coffee Ordering App will require us to send off the order summary in an email. • The app must launch an email app that exists on the device. • An intent is a message that requests some type of action to be performed by another app component. This could be a camera, email, Google Maps, or even an activity in another app.

Why are Intents Important? • Intents are a powerful part of the Android framework.

Why are Intents Important? • Intents are a powerful part of the Android framework. • They allow apps to leverage functionality that other apps provide. • They let developers be efficient and smart, because we're not reinventing what other apps already do extremely well. •

How Intents work • Intents are like tossing a ball to someone. • An

How Intents work • Intents are like tossing a ball to someone. • An app component can send an intent to another app or app component. Email Camera Map

Implicit Intents vs. Explicit Intents • There are two types of intents: Implicit and

Implicit Intents vs. Explicit Intents • There are two types of intents: Implicit and Explicit. • An intent can be sent to an explicit app or component. • An Intent can be sent implicitly.

Implicit Intent • An Implicit Intent specifies only the action to be performed. •

Implicit Intent • An Implicit Intent specifies only the action to be performed. • An Implicit Intent does not specify the explicit Android components which should be called. • As an analogy, imagine that we need you throw the ball, but we don't have to know who we're throwing it at, as long as there is someone there to catch it. Implicit – doesn’t care who catches it.

Implicit Intent Example • Google Maps provides a restaurant listing. • Clicking on Website

Implicit Intent Example • Google Maps provides a restaurant listing. • Clicking on Website sends an intent to display the restaurant website.

Implicit Intent Example Result • Google Maps app does not care which web browser

Implicit Intent Example Result • Google Maps app does not care which web browser handles the website launch request. • If the user only has one web browser installed on their device, then that app will automatically handle the request. • If the user has four web browsers installed on their device, then the Android framework will pop up a window to ask the user to just pick one.

Explicit Intent • Sometimes we want to be 100% sure that when we send

Explicit Intent • Sometimes we want to be 100% sure that when we send an intent. • Perhaps we want a very specific component to receive it. • We will discuss this more next week. Explicit– Does care who catches it.

What is the content if an Intent message? What is contained inside the ball

What is the content if an Intent message? What is contained inside the ball that represents an intent? • Think of an intent as a message specifying some operation to be performed. • Intents contain two primary elements and possibly extra elements. 1. The general action and 2. Data to act upon. • You can also specify additional information such as category, component, and extras, which is literally extra information.

Intent message • The general action and the Data to act upon are used

Intent message • The general action and the Data to act upon are used by Android to determine which app component can actually handle the intent. • TIP: Use Android’s the Common Intents guide for understanding the options when creating intents.

Example Intent messages • Map Example An implicit ntent that launches a map would

Example Intent messages • Map Example An implicit ntent that launches a map would contain the action ACTION_VIEW, and the data would be the latitude and longitude coordinates of the location that we want to see. Any app that is capable of displaying map using latitude and longitude coordinates can be used to handle the request, such as a map application. • Phone Calling Example An implicit intent to dial a phone number would require the action ACTION_DIAL and the corresponding data is the telephone number to call. The data format is in the form tel, colon, followed by the number. • Email Example An implicit intent to send an email would require the action ACTION_SENDTO and the corresponding data is the email address. The data format is in the form mailto: trish_cornez@redlands. edu.

What is a URI? • URI is a formatted string and is short for

What is a URI? • URI is a formatted string and is short for uniform resource identifier. • This is a standardized way to structure data so that it can be properly processed on the receiving end. • Think about it like a mailing address, it follows a structure so that it can be delivered properly. • When sending an intent, a URI is needed so that the data can be properly passed to the receiving app component.

Experiment : Lab 4 Exercise 1 Provide a button for the user to access

Experiment : Lab 4 Exercise 1 Provide a button for the user to access downtown Redlands in Google Maps. Use an implicit intent in the creation of the app.

Tasks: Map App Task 1: Locate the longitude and latitude of a place. geo:

Tasks: Map App Task 1: Locate the longitude and latitude of a place. geo: Latitude, Longitude. Task 2: Research how to create a simple geolocation intent https: //developer. android. com/guide/components/intents-common Take some time to experiment with writing code for different intents listed in the Common Intents guide. You can also do a web search for common intents. The documentation has some useful Task 3: Learn how you can to set the data for an intent. Task 4: Learn how you can check to see if the correct component is available on the device.

What did you find in your research? • Create a new Intent object and

What did you find in your research? • Create a new Intent object and pass it a ACTION_VIEW. • When composing our Intent, we will use an official intent action, such ACTION_VIEW or ACTION_SEND, which are defined as constants within the Intent class. • Set the data URI to be a certain set of latitude and longitude coordinates. a. Create a URI string. b. URI is set as the data field within this Intent object. • To prevent the app from crashing, check to see if the Intent can be handled. If there's no app component on the device that can handle this Intent, then we skip sending the Intent.