Android learning-implicit Intent-based communication

Source: Internet
Author: User

Android learning-implicit Intent-based communication
Intent implicit Communication

The Intent object can describe the task to be processed to the operating system. When using an explicit intent, we need to explicitly tell the activity class name to be started by the operating system.

The explicit intent created earlier is as follows:

Intent i = new intent(Context packageContext,  Class
   cls);startActivity(i);

To use an implicit intent, you only need to clearly describe our work intention to the operating system. The operating system starts activities that are declared to be competent for jobs. If the operating system finds multiple conforming activities, the user will see a list of optional applications, and then how to select them.

Implicit intents does not declare the specific class name of the component to be started, but declares an action to be executed. This action specifies what we want to do, such as viewing, editing, sending, or getting something. Intents usually includes some data when sending the action, such as the address you want to view or the mail information you want to send. The specific data type depends on the Intent we want to create, such as Uri or other specified data types, or even no data is required at all.

Composition of typical implicit intent

The following is a major component of an implicit intent that can be used to define our work tasks.

Action to perform

It is usually represented by constants in the Intent class.
For example, you can useIntent. ACTION_VIEW;
To send an email, you can useIntent. ACTION_SEND.

For example, view the webpage:

Uri webpage = Uri.parse(http://www.android.com);Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
Location of the data to be accessed

This may be a resource other than a device, such as a webpage URL, or a file URI or a content URI of a record in ContentProvider ).

If the data is a Uri, a simple Intent () constructor is used to define action and data.

For example, the following is an intent with a specified phone number:

Uri number = Uri.parse(tel:1234567);Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
Data Types involved in the operation

This refers to MIME-type data, such as text/html or audio/mpeg3. If an intent contains a certain type of data, the data type can be inferred from it.

Optional

If an operation is used to describe what to do, a category is usually used to describe when, where, or how to use an activity.
The Android. intent. category. LAUNCHER category of android indicates that the activity should be displayed in the top-level application LAUNCHER.
The android. intent. category. INFO category indicates that although the activity shows the package information to the user, it should not be displayed in the starter.

A simple and implicit intent used to view a website contains an Intent. ACTION_VIEW operation and the uri data of a specific URL.

Based on the above information, the operating system will start the applicable activity of the applicable application (if there are multiple applicable applications available, you can choose their own ).

Intent Filter Example

By setting the intent filter in the configuration file, the activity will claim to be an activity suitable for processing ACTION_VIEW.

To develop a browser application in response to the ACTION_VIEW operation, you must include the following intent filter in the activity declaration:

   
              
         
       
   
   

The DEFAULT category must be explicitly set in the intent filter. The action Element in the intent filter tells the operating system that the activity can process the specified task.
The DEFAULT category tells the operating system that the activity is willing to process a task. The DEFAULT category must be explicitly set in the intent filter.

The DEFAULT category is actually implicitly added to almost every implicit intent. (The only exception is the LAUNCHER category ).

If you have time to explore the use of this Intent filter ^_^

Implicit intent additional data

For example, an explicit intent can also contain extra information. However, when the operating system looks for an applicable activity, it does not use any extra attached to the implicit intent.

Some implicit intent that requires extra data. We can use putExtra () to add those data.By default, the system determines the appropriate MIME type based on the Uri data type. If we do not include a Uri in the intent, we usually need to use the setType () method to specify the data type that comes with the intent. MIME type is set to specify the activity that should accept this intent.

For example, send an email with an attachment:

Intent emailIntent = new Intent (Intent. ACTION_SEND); // This intent does not have a URI, so you need to declare text/plain MIME typeemailIntent. setType (HTTP. PLAIN_TEXT_TYPE); emailIntent. putExtra (Intent. EXTRA_EMAIL, new String [] {dengfengdecao@example.com}); // recipient emailIntent. putExtra (Intent. EXTRA_SUBJECT, Email subject); emailIntent. putExtra (Intent. EXTRA_TEXT, Email text); emailIntent. putExtra (Intent. EXTRA_STREAM, Uri. parse (content: // path/to/email/attachment); // You can append multiple items through ArrayList
  

Note: Please try to define the implicit Intent more accurately. For example, if you want to use the intent of ACTION_VIEW to display an image, you should also specify the MIME typeimage/*In this way, other apps (such as a map app) that can "View" other data types can be called by this intent.

Verify whether an App receives the Intent

Although the Android system ensures that each determined intent is received by one of the built-in apps (such as the Phone, Email, or Calendar app, however, we should verify whether an App accepts the intent before triggering an intent.

If no activity is triggered, the application crashes!

To verify whether a suitable activity will respond to this intent, You need to execute queryIntentActivities () to obtain the list of all the activities that can receive this intent. If the returned List is not empty, we can safely use this intent.

The following is a complete example of how to create an intent to view contacts in the address book. First, verify that an app can process the intent and then start it.

// The newly created implicit intent is composed of the operation and data acquisition location. // The operation is Intent. ACTION_PICK // The data location is ContactsContract. contacts. CONTENT_URI, contact address book Intent I = new Intent (Intent. ACTION_PICK, ContactsContract. contacts. CONTENT_URI); // check the activityPackageManager pm = getActivity () that can be returned by the device (). getPackageManager (); List
  
   
Activities = pm. queryIntentActivities (I, 0); boolean isIntentSafe = activities. size ()> 0; if (isIntentSafe) {// you need to obtain the returned data from the opened activity startActivityForResult (I, CONTACT_REQUEST_CODE );}
  

Note: This check must be performed before the first use. If it is not feasible, disable this function.

Start Activity with Intent

After intent is created and extra data is set, the intent is sent to the system by executing startActivity. If the system determines that multiple activities can process this intent, it will display a dialog box for the user to choose which app to start. If the system finds that only one app can process the intent, the system starts the app directly.

startActivity(intent);

Display the sharing App selection Interface

When an intent is passed in the form of startActivity () and multiple apps can be processed, you can select the default app to be started in the pop-up dialog box (by selecting the option box below the check box, as shown in ). This feature is useful when users have special preferences (for example, users always like to start an app to view webpages and always like to start a camera to take photos ).

However, if you want to bring up the selection interface every time, and you are not sure which app to start each time, such as the sharing function, you are not sure which app to choose to share. At this time, the Select dialog box needs to be displayed. (In this case, you cannot select the app to be started by default ).

To display the selection dialog box, usecreateChooser()To create Intent.

Intent intent = new Intent (Intent. ACTION_SEND );... // The title of the share dialog box String title = getResources (). getText (R. string. chooser_title); // create and start the selector Intent chooser = Intent. createChooser (intent, title); startActivity (chooser );

In this way, the response is listed.createChooser()And the title is specified.

Android learning-explicit Intent-based communication

 

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.