Google Android Developer Documentation Series-interacting with other applications enables users to jump to other applications

Source: Internet
Author: User

Sending the user to another app (enables users to jump to other applications)

This series of articles in my study of Google Developer documentation combined with Google translation and their own understanding of the writing, I hope to learn Android development friends bring some convenience, due to the limited level of personal translation, so the content contains the original text and translation, I hope that the browser combined understanding, so as not to step into the misunderstanding I may be wrong translation. In this thank http://android.xsoftlab.net/provide the image, I hope the reprint of the source http://blog.csdn.net/u014031072/article/details/51538673 convenient to view the latest blog

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 create a request to view the address using an Intent. The Android system then starts an app, that's able to show the address on a map.
One of the most important features of Android is that an application has the ability to jump to another app that wants to jump based on an "action" (behavior). For example, if your application wants to display an enterprise's address on a map, you do not need to create an activity specifically to display the map. You can request a display address by creating a intent (intent), and the Android system launches an application with the display map feature to display the address.

As explained 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 start. However, when your want to has a separate app perform an action, such as "View a map," You must use of an implicit intent.
As explained in the first lesson, to build your first application, you must use intent (intent) to navigate between your application's activities. You typically use explicit intent (show intent), which clearly defines the exact class name that you want to start the component. However, if you want to make another single application perform an operation, such as "View Map", you must use an implicit intent (implicitly 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.
This lesson will show you how to create an implicit intent (implicit intent) for a specific action, and how to use it to launch another application to launch an active interface to perform the specified action.

Build an implicit Intent (create an implicit intent (implicitly 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 get something. 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 might Not need data at all.
Implicit intent (implicit intent) does not define the class name of the startup component, but rather declares the action (behavior, action) to be performed. This action specifies what you want to do, such as viewing, editing, sending, or getting something. Intents often also contain data related to an action (action), such as the address you want to view, or the message content of an email you want to send. Depending on the intent (intent) you want to create, the data may be a URI or other data type, or intent (intent) you define may not need to add data at all.

If your data is a Uri, there's a simple Intent () constructor You can use define the action and data.
If your data is a URI, there is a simple intent () constructor that allows you to define the action (behavior, action) and data of the intent (intent).

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

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.
When your application calls StartActivity () this intention, the Telephony application starts the call for the given phone number.

Here is a couple other intents and their action and Uri data pairs:
Here are some other intent (intent) definitions and their respective action (behavior) and URI data pairs.

View a map ( view map ):

Mapon address//基于地址的地图上对应的点Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California"Ormapon latitude/longitude//或者基于经纬度的地图上对应的点// Uri location = Uri.parse("geo:37.422219,-122.08364?z=14"isnew Intent(Intent.ACTION_VIEW, location);

View a Web page ( viewing 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.
Other types of implicit intent need to provide "extra" data of different data types, such as strings. You can use the Putextra () method to add one or more additional data.

By default, the system determines the appropriate MIME type required by a intent based on the Uri of data that ' s included. If you don't include a Uri in the intent, you should usually use SetType () to specify the type of data associated with the Intent. Setting The MIME type further specifies which kinds of activities should receive the intent.
By default, the system determines the appropriate MIME type that it needs based on the URI data that intent (intent) contains. If you do not include a URI in intent (intent), you should typically use the Settype () method to specify the data type that the intent (intent) relates to. Setting the MIME type also specifies which types of activities can receive this intent.

Here is some more intents this add extra data to specify the desired action:
Here are some intent (intent) definitions and their respective corresponding actions (behaviors, actions) that contain additional data and expectations:

Send an email with an attachment ( send mail with attachments ):

Intent emailintent = new Intent (Intent. ACTION_send);The intent does not has a URI, so declare the"Text/plain"MIME type//This intent does not contain a URI, so it defines the MIME type of the "Text/plain" type Emailintent. SetType(HTTP. PLAIN_text_type);Emailintent. PutExtra(Intent. EXTRA_email, new string[] {"[email protected]"});//Recipients RecipientEmailintent. 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 a ArrayList of uris//you can also attach multiple items by passing the ArrayList of the URI

Create a calendar event ( creating calendar events ):

Intent calendarintent = new Intent (Intent. ACTION_insert, Events. CONTENT_uri);Calendar BeginTime = Calendar. getinstance(). Set( -,0, +,7, -);Calendar EndTime = Calendar. getinstance(). Set( -,0, +,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 the API level and higher.
Note: This calendar event Intent (Intent) only supports versions with API level 14 or higher.

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

Verify There is a app to receive the Intent (determine if an application exists to receive this 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.
Although the Android platform guarantees that certain intentions will be resolved to built-in application intent (such as phone, email, or calendar apps), you should also include a verification step before calling intent (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.
Note: If you call a intent (intent), but the device does not have the ability to manipulate the intent application, then your application will crash.

To verify there was an activity available that can respond to the intent, call Queryintentactivities () to get a list of act Ivities capable of handling your Intent. If The returned List is not empty, you can safely use the intent. For example:
To verify that an activity can respond to intent (intent), call the Queryintentactivities () method to get a list of activities that can handle the intent that you define. If the returned list is not empty, then you can safely use the intent of the definition. For example:

PackageManager packageManager = getPackageManager();List activities = packageManager.queryIntentActivities(intent,        PackageManager.MATCH_DEFAULT_ONLY);boolean isIntentSafe = activities.size0;

If Isintentsafe is true and then at least one app would respond to the intent. If it is false and then there aren ' t any apps to handle the intent.
If the value of Isintentsafe is true, then at least one application will respond to this intent. If it is false, then there will be no application to handle this intent.

Note: You should perform this check if your activity first starts in case you need to disable the feature 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).
Note: Before the user attempts to use the intent (intent) feature, you need to perform the above checks before the activity is first started, lest you need to disable the intent (intent) feature. If you know that a particular application can handle this intent (intent), you can also provide a link for users to download the appropriate application (see how to link to your product on Google Play).

Start an activity with the Intent (launch an active interface via Intent)

Once you has created your Intent and set the extra info, call StartActivity () to 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.
Once you have created your intent (intent) and set up additional information, call StartActivity () to send it to the system. If the system recognizes that more than one activity can handle the intent (intent), it will pop a dialog box for the user to select the application to use, 1, and if only one activity can handle the intent (intent), the system will start it immediately.

startActivity(intent);


Figure 1: Example of a dialog box that pops up when multiple activities are detected to respond to 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 int ENT, then start it:
The following is a complete example of how to create a view map of the intent (intent) and verify that there is an application that can respond to the intent of processing, and then start calling this intent (intent):

//Build The intent  //Create intent  Uri location = uri.parse ( "geo:0,0?q=1600+amphitheatre+ Parkway,+mountain+view,+california ""), Intent mapintent = new  Intent ( Intent.action_view, location); //Verify it resolves  //the solution to detect the intent (response activity list)  Packagemanager Packagemanager = Getpackagemanager (); List<resolveinfo> activities = Packagemanager.queryintentactivities (mapintent, 0 ); boolean  isintentsafe = activities.size () > 0 ; //Start an activity if it s safe  //if it is safe, start an active interface  if  (Isintentsafe) {startactivity (mapintent);}  
Show an app Chooser (shows an app selector)

Notice that if you start a activity by passing your Intent to StartActivity () and there are more than one app that Respo NDS to the intent, the user can select which app to use by default (by selecting a checkbox at the bottom of the dialog; s EE 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).
Note that when you call the StartActivity () method and pass in your intent (intent), the user can choose the application to use by default (by selecting the check box at the bottom of the dialog box if there is more than one application response intent) (see Figure 1). This way the system will default to the same application every time the action is executed, which is good, such as opening a Web page (the user may use only one web browser), or taking a picture (the user may prefer to use a webcam).

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 as shown in Figure 2. The Chooser dialog forces the user to select which app to the action every time (the user cannot select a default App for the action).
However, if the action that you want to perform can be handled by multiple applications, and the user might respond to a different application every time they like to start--such as "shared" behavior, for which a user may have several applications and can share the same project through them, you should explicitly display the selection dialog box, 2. The Select dialog box forces the user to select the application to respond to this action each time (the user cannot select the default app for the action).


Figure 2: A selection dialog box

To show the Chooser, create a Intent using Createchooser () and pass it to StartActivity (). For example:
To display the selector, create an intent to use Createchooser () and pass it to StartActivity (). For example:

Intent Intent= NewIntent (Intent.Action_send);...//Always use the string resources for UI text.//Always use strings as UI text//This says something like "Share this photo with"//This shows a statement like "Share photo to"StringTitle=Getresources ().GetString (R.string.Chooser_title);//Create intent to show Chooser//Create a selector intent (intent)Intent Chooser=Intent.Createchooser (Intent, title);//Verify The intent would resolve to at least one activity//Detect at least one activity can respond to this intent (intent)if(Intent.Resolveactivity (Getpackagemanager ())!= NULL) {startactivity (chooser);}

This displays a dialog with a list of apps that respond to the intent passed to the Createchooser () method and uses the SU Pplied text as the dialog title.
This displays a list of applications that can respond to the incoming intent (intent) passed to the Createchooser () method, using the provided text as the caption.

Google Android Developer Documentation Series-interacting with other applications enables users to jump to other applications

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.