210 likes | 506 Views
Intents. 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. Regular pants definition.
E N D
Fancy pants definition of Intent Apassive data structure holding an abstract description of an operation to be performed or a description of something that has happened and is being announced.
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
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.
How we’ve used Intents so far • An Intent object is passed toContext.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().
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.
Intents • Intents can contain • Component Name • Action • Data
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. Package name Fully Qualified Class Name com.example.project.app.FreneticActivity
Intents : Component Name • Explicit Intents need a component name • Implicit Intents do NOT need a component name
Intent : Action • A string naming the action to be performed
Intent : Data • The data to operate on, such as a person record in the contacts database, expressed as a Uri.
Examples of Action/Data pairs • ACTION_VIEWcontent://contacts/people/1 -- Display information about the person whose identifier is "1". • ACTION_DIALcontent://contacts/people/1 -- Display the phone dialer with the person filled in. • ACTION_VIEWtel: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_DIALtel:123 -- Display the phone dialer with the given number filled in. • ACTION_EDITcontent://contacts/people/1 -- Edit information about the person whose identifier is "1". • ACTION_VIEWhttp://www.google.com -- Display the browser with the given url filled in.
Example Intents : Long Way Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://www.smu.edu")); startActivity(intent);
Example Intents : Compact Way Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); startActivity(intent);
Show Phone Number in Dialer App ////Show Phone number in Dialer App Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:2147681234")); startActivity(intent);
Google Search ////Open browser and perform a google search Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY, "SMU"); startActivity(intent);
Open Address in Google Maps //Open Google Maps and load a map for a specific geo location Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:32.84453,-96.78534")); startActivity(intent);
Compose an Email //Compose an email with subject and body filled in Intent 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);
Choose an Activity to share data with //Send data to any app that accepts text/plain mime type Intent 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
Edit Contact //Edit a contact in your contacts list Intent intent = new Intent(Intent.ACTION_EDIT, Uri.parse("content://contacts/people/1")); startActivity(intent);