Android uses intent to interact with other apps

Source: Internet
Author: User

Objective:

On a blog to everyone to talk about the definition of intent, classification, properties and functions, I believe that everyone on the role of intent in Android has been clear, this blog will be for everyone to talk about intent usage.


An important feature of the Android system is that an application can invoke another application to complete the user's request action. For example, your application needs to show the user a geographic location on the map, you do not have to implement the map function in your application, but instead create a intent that shows the location, send it out, and the Android system launches applications that can handle the request. Also for example: You use Baidu cloud disk download a PDF document, you click Open this document when the Baidu Cloud disk is unable to open, but perhaps you installed on the system has other can open the PDF document reader, this time will pop up a dialog box, listing can open the PDF document of the application, You are free to choose an application to open the document you downloaded.


Use implicit intent:

The implicit intent does not indicate the name of the component to start, but rather declares the action performed, specifying what you want to do, such as displaying (view), editing (edit), sending (send), getting something (get something), and so on. Intent often comes with some data, such as the address you want to view, the content of the email, and so on. The data form depends on what you want to do, the data can be a URI, or it can be one of the other data types (basic data types or objects). The data is not required, and your intent can be contained without data.

If your data is a URI, you can use intent (Action,uri) constructor to easily create intent, the following example is to create a call intent, the data is a URI containing a phone number:

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

When you call StartActivity (Intent Intent) in your application and pass in the Intent above, the phone application will call the phone number you specified above. Here are two examples of implementing other functions using intent.

To view a location on a map app:
Map point based to address Uri 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 level Intent mapintent = new Intent (Intent.action_view, location);

To view the requested URL on a browser:

Uri webpage = uri.parse ("http://www.android.com"); Intent webintent = new Intent (Intent.action_view, webpage);

The above intent are not with data, but sometimes intent need to be accompanied by a date, the data can be a string, can be a basic data type, you can call the Putextra () method to set multiple data.

Intent data is a data type, which is called MIME type, and by default the system uses the URI data contained in the data to determine the MIME type of the data, and if the data does not contain a URI, you need to call Settype () to set the MIME type of the intent data. Setting the MIME type further specifies which types of activity can respond to this intent.


Note: MIME: Full name Multipurpose Internet Mail Extensions, multi-function Internet Message expansion service. It is a Multipurpose Internet Mail Expansion protocol that was first applied to the e-mail system in 1992, but was later applied to the browser. A MIME type is a type of file that sets an extension that is opened by an application, and the browser automatically opens with the specified application when the extension file is accessed. Many are used to specify some client-customized file names, as well as some ways to open media files. In Android, the MIME type of the file is used to determine which applications can process the files and use one of the applications (if there are multiple optional applications, the user must specify one) to handle.

Here is an example of the intent with the data: send an email with an attachment:
Intent emailintent = new Intent (intent.action_send);//The Intent does not has a URI, so declare the "Text/plain" MIME ty PE emailintent.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 calendar events (API level and higher):

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: Your intent is as detailed as possible about your intentions and it is important to set the intent data MIME type. For example, if you want to open a picture through Action_view intent, you should set the MIME type to "image/*" so that you can block calls to other types of applications, such as the map app, but rather that only applications that can view the image respond to this intent.

Verify that the intent is valid:

Although the Android system built-in applications (such as phone, mail, calendar) can guarantee that some intent will definitely be responded to, but before you send a intent it is best to verify that intent can be processed by the system. If you send a intent without an application to handle, then your application will appear crash phenomenon.

You can call the Queryintentactivities () method to verify that intent has an activity response, and queryintentactivities () returns a list of activity that can respond to the intent. If the list is not empty, then you can safely send this intent. The code to verify the intent is as follows:

Packagemanager Packagemanager = Getpackagemanager (); List activities = packagemanager.queryintentactivities (Intent,        packagemanager.match_default_only); boolean Isintentsafe = activities.size () > 0;
The following example is an example of a complete launch map application that contains security checks for pre-launch intent:
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 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 App selector:

Sometimes when you call StartActivity () to request activity from the system to process your request, there will be multiple application responses, and the user needs to select an application to handle the intent request, which is a very good experience. Users are free to choose an application, or they can specify which application will respond to the request by default, such as taking pictures, viewing pictures, and surfing the Internet. To show the user that the app selector needs to call Creatchooser () to create your intent, for example:

Intent Intent = new Intent (intent.action_send);  Always use the string resources for UI text. This says something like "Share this photo with" String title = Getresources (). getString (R.string.chooser_title);//Cre Ate intent to show chooser Intent Chooser = Intent.createchooser (Intent, title); Verify the intent would resolve to at least one activity if (Intent.resolveactivity (Getpackagemanager ()) = null) {    StartActivity (chooser);}

The example above will pop up a dialog with many applications that are capable of responding to this intent application.

This article is original, reproduced please indicate the source, offenders must investigate!

Focus on the public platform: the Programmer Interaction Alliance (coder_online), you can get the first original technical articles, and (Java/c/c++/android/windows/linux) technology Daniel Friends, online communication programming experience, get programming basics, Solve programming problems. Programmer Interactive Alliance, Developer's own home.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android uses intent to interact with other apps

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.