Android-bringing users to another application (2)

Source: Internet
Author: User

Verify that there is an application to receive intent objects

Although the Android platform ensures that the determined intent object will be received by the built-in applications (such as phone, email, calendar, and other applications), before calling an intent object, you should always include a verification step.

Warning if you call an intent object and the application that cannot process this intent object on the device exists, your application will crash.

Call queryintentactivities () to obtain the list of activities that can process your intent object, so that you can check whether any activity can effectively respond to this intent object. If the list returned by this method is not empty, you can safely use this intent object. For example:

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

If isintentsafe is true, at least one application can respond to this intent object. If it is false, no application can process this intent object.

Note: When your activity is started for the first time, you should perform this check. If your intent application does not exist, you should disable this function. If you know the specific application that can process this intent object, you can also provide users with a link to download this application.

Use intent object to start Activity

Once you create an intent object and set additional information, you can call startactivity () to send it to the system. If the system identifies that multiple activities can process the intent object, the system displays a dialog box for the user to select the application to use. Dialog Box 1 is displayed. If only one activity can process this intent object, the system starts it directly.

Figure 1. Select dialog box displayed when there are multiple activities that can process intent objects.

The following is a complete example of creating an intent object that displays a map. Before starting it, you will check whether the application that processes this intent object exists:

// Build the 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
Packagemanager =getPackageManager();
List <resolveinfo> activities = packagemanager. queryintentactivities (mapintent, 0 );
Boolean isintentsafe = activities. Size ()> 0;

// Start an activity if it's safe
If (isintentsafe ){
Startactivity (mapintent );
}

Show application Selector

Note: When you pass the intent object to the startactivity () method to start the activity, if multiple applications can respond to the intent object, a dialog box is displayed, select the default handler (set by selecting the checkbox at the bottom of the dialog box, as shown in 1 ). This is advantageous for every time you want to use the same application to perform the same operation. For example, when you open a browser (you only want to use one browser ), or take a photo (the user uses the favorite camera ). However, if you want to perform operations that can be processed by multiple applications, and each time you like to use different applications, such as "share" operations, each time you share content, different applications may need to be used. Therefore, each time a user needs to select an application for the operation (the user cannot select the Default Application for the operation)

Use the createchooser () method to create an intent object and pass it to the startactivity () method to display this selector. For example:

Intent intent
= Newintent (intent. action_send );
...
 
// Always use string resources for UI text. This says something like "share this photo"
String title = getresources (). gettext (R. String. chooser_title );
// Create and start the chooser
Intent chooser = intent. createchooser (intent, title );
Startactivity (chooser );

In this way, an application list that can process the intent object is displayed, and the provided text is used as the title of the dialog box.

Figure 2. The app selector dialog box displayed when you use the createchooser () method. This dialog box ensures that all applications that can process the intent object are displayed each time.

 

Related Article

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.