Intent transmission mechanisms

Source: Internet
Author: User

Intent transmission mechanisms

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 intent) of Context or startActivityForResult (Intent 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 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 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 intent) to start directly.

(2) Use startActivityForResult (Intent intent, Int requestCode), setResult (int resultCode, Intent intent), onActivityResult (int requestCode, int resultCode, Intent intent)

A starts the process of B through startActivityForResult.

A. Call startActivityForResult (Intent 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 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 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.


[Android Development]: several data transmission methods between activities

1. intent is used to transmit data Intent tables. In many cases, Android Intent is used to transmit data between various activities. This is also an official data transfer method for Android. Requirement 1: jump from an Activity (IntentDemo) to another Activity (Other). The Demo of the Data Program passed by Intent is as follows: IntentDemo. javapackage com. android. intentdemo; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. button; public class IntentDemo extends Activity {private Button button; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); initComponent (); button. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (IntentDemo. this, Other. class); // transmits data Intent in intent. putExtra ("name", "AHuier"); intent. putExtra ("age", 22); intent. putExtra ("address", "XiaMen"); // start Intent startActivity (intent) ;}};} private void initComponent () {button = (Button) findViewById (R. id. button) ;}} other. javapackage com. android. intentdemo; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. textView; public class Other extends Activity {private TextView textView; @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. other); initComponent (); Intent intent = getIntent (); int age = intent. getIntExtra (...... remaining full text>

How to Use intent to transmit int data

In android, intent objects do not support direct int data transfer. There are two ways to solve this problem: First, data type conversion, however, in some special cases, this method is not applicable. Second, the bundle object is used to encapsulate data for transmission, for example, the sender: Bundle bundle = new Bundle (); bundle. putInt ("deptname", 3); intent. putExtras (bundle); to solve the problem.

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.