Android (Intent Learning)

Source: Internet
Author: User

Intent is a messaging object, Intent can facilitate communication between components in a number of ways, with three basic use cases:

Start acitivity:

Activity represents a screen in your app, and you can start a new activity instance by passing intent to startactivity (). Intent describes the activity to be started and carries any necessary data.
Start the service:

  Serviceis a component that performs operations in the background without using the user interface. By Intent passing it on startService() , you can start the service to perform a one-time operation (for example, download a file).

Delivery Broadcast:

Broadcasts are messages that can be received by any application. Various broadcasts are delivered for system events, such as when the system starts or when the device starts charging. Intentyou can pass the sendBroadcast() broadcast to another app by passing it to, sendOrderedBroadcast() or sendStickyBroadcast() .

  

Intent type

Explicit Intent: Specifies the component to start by name (fully qualified class name).

Stealth Intent: Does not specify a specific component, but rather declares the general action to be performed, allowing components in other applications to handle it. For example, if you want to show a location to a user on a map, you can use implicit Intent to request another app with this feature to display the specified location on the map.

  

The main information contained in the intent:
Component Name (optional, if no component name is implicit intent)

Use the setComponent() , setClass() ,, setClassName() or constructor to set the Intent component name.

Operation

Specifies a string of common actions to perform, specifying your own actions for Intent to use within your app (or for other apps to invoke components in your app). However, you should typically use Intent an action constant defined by a class or other framework class. Here are some common actions to start the Activity:

  

    ACTION_VIEW
If you have some information that an activity can display to users (for example, photos you want to view using the Gallery app, or you want to use the address viewed by the Map app), use Intent to use this action in startActivity() conjunction with.
     ACTION_SEND
This is also known as the "shared" Intent. If you have data that users can share through other apps, such as an email app or a social sharing app, you should use Intent to combine this action with startActivity() .

Use setAction() Intent the or constructor to specify an action for Intent.

  

Data

When creating Intent, specifying a data type (its MIME type) is often important in addition to specifying a URI. For example, an Activity that can display an image may not play an audio file, even if the URI format is very similar. Therefore, specifying the MIME type of the data helps the Android system find the best component to receive Intent. Sometimes, however, MIME types can be inferred from URIs, especially when the data is a content: URI. This indicates that the data resides in the device and is ContentProvider controlled, which makes the data MIME type visible to the system.

To set the data URI only, call setData() . To set only MIME types, call setType() . If necessary, you can use setDataAndType() both to explicitly set both.

Category

A string that contains additional information that should handle the Intent component type. Common categories:
  CATEGORY_BROWSABLE The target Activity allows itself to be launched through a Web browser to display links to referenced data, like or e-mail.

  CATEGORY_LAUNCHER The activity is the initial activity of the task, listed in the Application Launcher of the system.

  

For a complete list of categories, see Intent class descriptions.

Use the addCategory() specified category.

An explicit Intent example

Explicit Intent refers to the Intent that is used to start a particular application component (for example, a specific Activity or service in an app).

  

// Executed in a Activity, so ' this ' is the Context // the FILEURL is a string URL, such as " Http://www.example.com/image.png " New Intent (This, downloadservice.  Class);d ownloadintent.setdata (Uri.parse (FILEURL)); StartService (downloadintent);
An implicit Intent example

The implicit Intent specifies the ability to invoke any app operation on a device that can perform the appropriate action. Using implicit Intent is useful if your app can't do this and other apps can, and you want the user to pick the app you want to use.

  

// Create The text message with a string New Intent (); sendintent.setaction (intent.action_send); Sendintent.putextra (Intent.extra_text, textmessage); Sendintent.settype ("Text/plain"); // Verify that the intent would resolve to an activity if NULL ) {    startactivity (sendintent);}



Original link: https://developer.android.google.cn/guide/components/intents-filters.html#Building

Android (Intent Learning)

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.