Several data transmission mechanisms of intent

Source: Internet
Author: User

1. What is intent?

 

Intent refers to the view, which is the carrier for communication between different components in Android applications. When Android needs to connect different components during runtime, it usually needs to be implemented by intent. Intent can start another activity in the application or a service component, and send a broadcast message to trigger broadcastreceiver in the system. The communication between the activity, service, and broadcastreceiver components uses intent as the carrier.

(1) When starting an activity, you can call startactivity (intent) of context or startactivityforresult (intent, int requestcode) of the activity. The intent parameter encapsulates the information of the activity to be started.

(2) when starting a service, you can call the startservice (intent) method of context or the bindservice (intent service, serviceconnection Conn, int flags) method, the intent parameter encapsulates the information of the target service to be started.

(3) When a broadcastreceiver is triggered, you can call sendbroadcast (intent), sendstickybroadcast (), or sendorderedbroadcast () of context (), the intent parameter in the three methods encapsulates the information of the target broadcastreceiver to be triggered.

2. What information does intent store: (for your understanding) the information stored in Intent includes the information of this activity and the information of the activity to be redirected.

3. explicit intent and implicit intent explicit intent: determine that the unique element of the target component is the component name. More information is transferred within the application.

4. Intent parsing mechanism: Android uses intent action, type, and category to determine. Implicit intent: The component name is not explicitly specified. The Android system helps the program find the component that best matches the intent request intent. Android uses intentfilter to find objects related to implicit intent. More information is transferred in different applications, such as redirecting to the music player and opening the image library.

5. How intent starts the activity.

Startactivity () is an asynchronous operation that enables both the new activityb and the original activitya. Startactivityforresult () is a synchronization operation. activitya blocks itself after activating a new activityb. activitya continues to execute activitya after activityb exits and performs the next operation according to the result returned by activityb.

(1) Use startactivity (intent) to start directly.

(2) Use startactivityforresult (intent, int requestcode), setresult (INT resultcode, intent), onactivityresult (INT requestcode, int resultcode, intent)

A starts the process of B through startactivityforresult.

A. Call startactivityforresult (intent, int requestcode) in a first ).

The intent parameter represents a normal intent. For example, if you want to jump from A to B, you can use intent = new intent (. CThis, B. class); intent specifies the jump to B, and contains the data that a passes to B. Requestcode> = 0. If a needs to jump to C, D, and so on, requestcode corresponds to different values. For example, to jump from A to B, C, D, etc., you can use 1, 2, 3, and so on to distinguish. In onactivityresult (), you need to distinguish the results returned by each activity.

For example:

  //startActivityForResult(intent,1);  --1:B   //startActivityForResult(intent,2);  --2:C   //startActivityForResult(intent,3);  --3:C      protected void onActivityResult(int requestCode, int resultCode, Intent data) {           switch (requestCode) {                 case 1:                       ...                       break;                 case 2:                       ...                       break;                  case 3:                       ...                       break;               default:                break;   } 

  

  

B. setresult (INT resultcode, intent) in B ).

Resultcode is used to differentiate several different returned results of B.

The intent (in this example) parameter indicates that A is returned, which is mainly used to return additional data and will be passed to onactivityresult () of ()

C. Finally, onactivityresult (INT requestcode, int resultcode, intent data) is called in)

Requestcode: identifies the activity that is passed back.

Requestcode: Specifies the activity to be passed.

Data: The intent returned when setresult is called in B.

6. How intent transmits data.

(1) putxxxextra ("key", "value ");

(2) first create a bundle object, which is used to carry data. Then call intent. putextras (bundle );

(3) intent requires that the transmitted data be implemented either in the serializable interface or parcelable interface.

7. The data type passed by intent.

(1) integer arraylist

(2) string-type arraylist

(3) Other types (other types seem to be serializable only). You must reference the serializable interface. The serializable interface is a universal data storage and reading interface provided by Java. Any type can be saved to a file as long as the serializable interface is implemented, or data streams can be sent to other places through the network. It can also be transmitted to other programs in the system through pipelines. Serializable not only can be used locally, but also can be operated through the network (RMI ). The advantage is that the differences between operating systems are automatically shielded.

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.