"Android official Docs" interacts with other apps

Source: Internet
Author: User

Send user to another appyou should ALSO READ
    • Content sharing

One of Android's most important features are an app's ability to send the user to another app based on an "action" it would Like to perform. For example, if your app had the address of a business so you'd ' d like-to-show on a map, and you don ' t had to build an activi Ty in your app, that shows a map. Instead, you can send a out a request to view the address using a Intent . The Android system then starts an app, that's able to view, the address on a map.

As shown in the first class, Building Your First app, you must use intents to navigate between activities in Your own app. You generally a explicit intent, which defines the exact class name of the component you want to STA Rt. However, when your want to has a separate app perform an action, such as "View a map," You must use an implicit intent .

This lesson shows what to create an implicit intent for a particular action, and how to use it to start an activity tha T performs the action in another app.

Build an implicit Intent

Implicit intents do not declare the class name of the the component to start, but instead declare a action to perform. The action specifies the thing you want to do, such as view, edit, send, or getSomethi Ng. Intents often also include data associated with the action, such as the address of your want to view, or the e-mail message you Want to send. Depending on the intent-want to create, the data might is a Uri , one of the several other data types, or the intent Migh t need data at all.

If your data is a Uri , there's a simple Intent() constructor you can use define the action and data.

For example, here's how-to-create an intent-initiate a phone call using the Uri data to specify the telephone number:

Uri number = Uri.parse ("tel:5551234"), Intent callintent = new Intent (intent.action_dial, number);

When your apps invokes this intent by calling startActivity() , the phone app is initiates a call to the given Phone number.

Here is a couple other intents and their action and Uri data pairs:

    • View a map:
      Map Point based to Addressuri location = Uri.parse ("Geo:0,0?q=1600+amphitheatre+parkway,+mountain+view,+california" )//or map point based on latitude/longitude//Uri location = Uri.parse ("geo:37.422219,-122.08364?z=14"); Z param is zoom levelintent mapintent = new Intent (Intent.action_view, location);
    • View a Web page:
      Uri webpage = uri.parse ("http://www.android.com"); Intent webintent = new Intent (Intent.action_view, webpage);

Other kinds of implicit intents require "extra" data, provide different data types, such as a string. You can add one or more pieces of extra data using the various putExtra() methods.

By default, the system determines the appropriate MIME type required by a intent based on the Uri data that ' s included. If you don't include a Uri in the intent, you should usually use to setType() specify the type of data associated with the Tent. Setting The MIME type further specifies which kinds of activities should receive the intent.

Here is some more intents this add extra data to specify the desired action:

  • Send an e-mail with an attachment:
     Intent emailintent = new Intent (intent.action_send);//The Intent does not having A URI, so declare the "Text/plain" MIME typeemailintent.settype (HTTP. Plain_text_type); Emailintent.putextra (Intent.extra_email, new string[] {"[email protected]"}); Recipientsemailintent.putextra (Intent.extra_subject, "Email SUBJECT"); Emailintent.putextra (Intent.extra_text, " Email message text "); Emailintent.putextra (Intent.extra_stream, Uri.parse (" content://path/to/email/attachment ")); /You can also attach multiple items by passing an ArrayList of Uris 
  • Create A calendar event:
     Intent calendarintent = new Intent (Intent.action_insert, Events.content_uri); Calendar beginTime = Calendar.getinstance (). Set (2012, 0, 19, 7, 30); Calendar endTime = Calendar.getinstance (). Set (0, N, ten, +); Calendarintent.putextra (Calendarcontract.extra_ Event_begin_time, Begintime.gettimeinmillis ()); Calendarintent.putextra (Calendarcontract.extra_event_end_time, Endtime.gettimeinmillis ()); Calendarintent.putextra (Events.title, "Ninja class"); Calendarintent.putextra ( Events.event_location, "Secret Dojo"); 

    Note:  this Intent for a calendar event was supported only with API level and Highe R.

Note: It's important that's define your to be as Intent specific as possible. For example, if you want-to-display an image using ACTION_VIEW the intent, you should specify a MIME type of image/* . This prevents apps-can "view" other types of data (like a maps app) from being triggered by the intent.

Verify there is a App to Receive the Intent

Although the Android platform guarantees that certain intents would resolve to one of the built-in apps (such as the Phone, Email, or Calendar app), you should always include a verification step before invoking an intent.

Caution: If you invoke an intent and there are no app available on the device that can handle the intent, your app would crash.

To verify there are an activity available that can respond to the intent, call to queryIntentActivities() get a list of activities capable of Handling your Intent . If The returned isn't List empty, you can safely use the intent. For example:

getPackageManager(); List<resolveinfo> activities = packagemanager.queryintentactivities (intent, 0); Boolean isintentsafe = Activities.size () > 0;

If isIntentSafe true is and then at least one app would respond to the intent. If it false is and then there aren ' t any apps to handle the intent.

Note: Should perform this check if your activity first starts in case you need to disable the feature that uses the intent Before the user attempts to use it. If you know of a specific apps that can handle the intent and you can also provide a link for the user to download the app (SE e How to link to your product on Google Play).

Start an Activity with the Intent

Figure 1. Example of the selection dialog that appears when more than one app can handle an intent.

Once you has created your Intent and set the extra info, call to startActivity() send it to the system. If the system identifies more than one activity so can handle the intent, it displays a dialog for the user to select WH Ich app to use, as shown in Figure 1. If There is only one activity that handles the intent, the system immediately starts it.

StartActivity (Intent);

Here's a complete example so shows how to create an intent to view a map, verify that's app exists to handle the intent , then start it:

getPackageManager(); List<resolveinfo> activities = packagemanager.queryintentactivities (mapintent, 0); Boolean isIntentSafe = Activities.size () > 0;  Start an activity if it ' s Safeif (isintentsafe) {    startactivity (mapintent);}
Show an App Chooser

Figure 2. Example of the Chooser dialog that appears if you use to ensure that the user was always createChooser() shown a list of apps that R Espond to your intent.

Notice If you start a activity by passing your  Intent  to startactivity () & Nbsp;and there is more than one app that responds to the intent, the user can select which app to use by default (by Selec Ting a checkbox at the bottom of the dialog; See Figure 1). This was nice when performing a action for which the user generally wants to use the same app every time, such as when Ope Ning a Web page (users likely use just one web browser) or taking a photo (users likely prefer one camera). However, if the action to is performed could be handled by multiple apps and the user might prefer a different app each TI Me-such as a "share" action, for which users might has several apps through which they might share an item-you should exp Licitly Show a chooser dialog, which forces the user to select which app to use for the action every time (the user cannot Select a default app for the action).

To show the Chooser, create a Intent using and createChooser() pass it to startActivity() . For example:

Intent Intent = new Intent (intent.action_send) ...//Always use string resources for UI text. This says something like "Share this photo with" String title = Getresources (). GetText (R.string.chooser_title);//Create an D start the Chooserintent chooser = Intent.createchooser (Intent, title); StartActivity (chooser);

This displays a dialog with a list of apps that respond to the intent passed to the createChooser() method and uses the supplied text As the dialog title.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.