Android --- 8 --- Intent and use Intent to transmit data, androidintent

Source: Internet
Author: User

Android --- 8 --- Intent and use Intent to transmit data, androidintent

Intent is an important way to interact with each component in the Android program. It not only specifies the current group

You can also transfer data between different components. Intent can be used to start activities and

Services, as well as sending and broadcasting

Intent is a runtime binding mechanism that can connect two different components while the program is running. The intent program can express a request or willingness to Android. Android will select an appropriate component for the request based on the desired content.

The communication between these components is mainly completed with Intent assistance. Intent describes the actions, actions involving data, and additional data of an application. Android identifies the corresponding components based on the description of the Intent, pass Intent to the called component and complete the call of the component.

Therefore, Intent acts as a media intermediary here, providing information about self-check and mutual calls to decouple callers from callers.

For example, in an application maintained by a contact, when we click a contact on the screen of a contact list, we hope to display the Contact Details screen.

 

To achieve this purpose, listActivity needs to construct an Intent, which is used to tell the system the action we want to view. The object corresponding to this action is "a contact ", call startActivity (Intent intent) to pass in the constructed Intent. Based on the description in this Intent, the system finds the Activity that meets the Intent requirement in ManiFest. The system calls the Activity that is found, that is, detailActivity is passed into Intent. detailActivity performs the corresponding operation according to the description in this Intent.

 

First, there is a layout interface. There is only one button. When you click the button, the details of the contact will be displayed on the other interface.

Create MainActivity first:


Package com. example. intentdemo; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button;/*** use Intent to transmit data. * For example, when a mobile phone contact is clicked, the details of this person will be displayed * Of course, it is completed through another interface * @ author Caesar **/public class MainActivity extends Activity implements OnClickListener {private Button button; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. button); button. setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // create an Intent, it indicates that the event will jump from MainActivity to OtherActivity activity. // The first parameter indicates the context of the startup activity, and the second parameter indicates the target activity to be redirected. class type Intent intent = new Intent (MainActivity. this, OtherActivity. class); // input data Intent for intent. putExtra ("name", "zhangsan"); intent. putExtra ("age", 15); intent. putExtra ("address", "Beijing"); intent. putExtra ("tel", "121212121212"); // start the activity startActivity (intent );}}

Because the program needs to jump to OtherActivity, you should create an OtherActivity class to inherit the Activity, re-write the onCreate method, and load the other layout. Now there is no other layout, you need to first create.

Because OtherActivity is mainly used to display information, there is only one TextVIew in the other layout for displaying information.

Other. xml


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/msg"        android:layout_width="fill_parent"        android:layout_height="wrap_content" /></LinearLayout>

OtherActivity. java


Package com. example. intentdemo; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. textView; public class OtherActivity extends Activity {private TextView textView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. other); textView = (TextView) findViewById (R. id. msg); // get Intent intent = getIntent (); // get the intent information int age = Intent. getIntExtra ("age", 0); String name = intent. getStringExtra ("name"); String address = intent. getStringExtra ("address"); String tel = intent. getStringExtra ("tel "); string Info = "name -->" + name + "\ n" + "age -->" + age + "\ n" + "address -->" + address + "\ n "+" TEL: "+ tel; // display textView. setText (Info );}}


We need to add permissions for OtherActivity in the registry:

In application, add the following content to the first activity: <activity android: name = ". OtherActivity">

 

OK. Run:







OK.

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.