20
Intents

Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Embed Size (px)

Citation preview

Page 1: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Intents

Page 2: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Fancy pants definition of Intent

A passive data structure holding an abstract description of an operation to be performed or a description of something that has happened and is being announced.

Page 3: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Regular pants definition

• Intents are asynchronous messages which allow Android components to request functionality from other components of the Android system.

• Allows developers to leverage the capability of other apps

Page 4: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Using Intents

• Intents are sent to the Android system via the startActivity().

• Depending on how the Intent was constructed, the Android system will run an receiver and determine possible components that can be started.

• If several components have registered for the same intents the user can decide which component should be started.

Page 5: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

How we’ve used Intents so far

• An Intent object is passed to Context.startActivity() or Activity.startActivityForResult() to launch an activity or get an existing activity to do something new.

• It can also be passed to Activity.setResult() to return information to the activity that called startActivityForResult().

Page 6: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Explicit vs Implicit Intents

• Explicit intents explicitly define the component which should be called by the Android system by using the Java class as the identifier.

• Implicit intents specify the action which should be performed and optional data which provides data for the action.

Page 7: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Intents

• Intents can contain– Component Name– Action– Data

Page 8: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Intents : Component Name

• The name of the component that should handle the intent.

• The name of the component should be a fully qualified class name of the target component its package name.

com.example.project.app.FreneticActivity

Package name Fully Qualified Class Name

Page 9: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Intents : Component Name

• Explicit Intents need a component name

• Implicit Intents do NOT need a component name

Page 10: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Intent : Action

• A string naming the action to be performed

Page 11: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Intent : Data

• The data to operate on, such as a person record in the contacts database, expressed as a Uri.

Page 12: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Examples of Action/Data pairs• ACTION_VIEW content://contacts/people/1 -- Display information about the person whose

identifier is "1".

• ACTION_DIAL content://contacts/people/1 -- Display the phone dialer with the person filled in.

• ACTION_VIEW tel:123 -- Display the phone dialer with the given number filled in. Note how the VIEW action does what is considered the most reasonable thing for a particular URI.

• ACTION_DIAL tel:123 -- Display the phone dialer with the given number filled in.

• ACTION_EDIT content://contacts/people/1 -- Edit information about the person whose identifier is "1".

• ACTION_VIEW http://www.google.com -- Display the browser with the given url filled in.

Page 13: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Example Intents : Long WayIntent intent = new Intent();intent.setAction(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://www.smu.edu"));startActivity(intent);

Page 14: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Example Intents : Compact WayIntent intent = new Intent(Intent.ACTION_VIEW,

Uri.parse("http://www.google.com"));startActivity(intent);

Page 15: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Show Phone Number in Dialer App////Show Phone number in Dialer AppIntent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:2147681234"));startActivity(intent);

Page 16: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Google Search////Open browser and perform a google searchIntent intent = new Intent(Intent.ACTION_WEB_SEARCH);intent.putExtra(SearchManager.QUERY, "SMU");startActivity(intent);

Page 17: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Open Address in Google Maps//Open Google Maps and load a map for a specific geo locationIntent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:32.84453,-96.78534"));startActivity(intent);

Page 18: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Compose an Email//Compose an email with subject and body filled inIntent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:?subject=" + Uri.encode("Mixed Berry Recipe") + "&body=" + Uri.encode("I found this awesome recipe")));startActivity(intent);

Page 19: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Choose an Activity to share data with

//Send data to any app that accepts text/plain mime typeIntent intent = new Intent(Intent.ACTION_SEND);intent.setType("text/plain");intent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");intent.putExtra(Intent.EXTRA_TEXT, "Body Here");//This will create a chooser pop-up that allows the user to select from a list of options for how they//want to handle the intent (which app to use).startActivity(Intent.createChooser(intent, "Share this recipe with")); //Add special text to chooser pop-up

Page 20: Intents. Fancy pants definition of Intent A passive data structure holding an abstract description of an operation to be performed or a description of

Edit Contact//Edit a contact in your contacts listIntent intent = new Intent(Intent.ACTION_EDIT, Uri.parse("content://contacts/people/1"));startActivity(intent);