Android learning notes (4) Intent basic learning, androidintent

Source: Internet
Author: User

Android learning notes (4) Intent basic learning, androidintent
Corresponding to video tutorial Lesson 7th

This course explains intent, which is an abstract description of the action and indicates the intent. Once an intent is created, an action can be executed for it. inten is the essence of android development.
1. click the button to call a specified number.

Btn1.setOnClickListener (listener); private OnClickListener listener = new OnClickListener () {Intent intent = new Intent (); @ Overridepublic void onClick (View v) {intent. setAction (Intent. ACTION_CALL); // specifies the intent action that calls the intent. setData (Uri. parse ("tel: 110"); // set the phone number startActivity (intent); // start Toast. makeText (MainActivity. this, "call 110", Toast. LENGTH_SHORT ). show (); // display prompt }};
2. click the button to send a text message to a specified number.
Btn1.setOnClickListener (listener); private OnClickListener listener = new OnClickListener () {Intent intent = new Intent (); @ Overridepublic void onClick (View v) {case R. id. button1: intent. setAction (Intent. ACTION_SENDTO); intent. setData (Uri. parse ("smsto: 110"); intent. putExtra ("sms_body", "OK, harassment ~~ "); // Note the format startActivity (intent); Toast. makeText (MainActivity. this, "send a text message to 110", Toast. LENGTH_SHORT ). show (); // display prompt }};
3. Start another activity in one activity
The most basic content of a set of activities should be:
1. activity Class (xxActivity. java)
2. layout file (layout/xxx. xml)
3. register the activity in the configuration file.
<Activity
Android: name = "xxxxx"
Android: label = "xxxx">
..................
</Activity>

With these basic elements, you can call this activity in another activity.

// For convenience, place the jump in a button click event. intent intent = new Intent (); intent. setClass (MainActivity. this, SecondActivity. class); // parameter 1 is packageContext, generally pointing to MainActivity itself; parameter 2 is cls pointing to the classstartActivity (intent) of the activity to be called );
Add the configuration file:
<Activity android: name = ". SecondActivity" // same as activity name android: label = "@ string/app_name"> // modify it as needed </activity>
The above are the most basic content. If your SecondActivity has a function, you can jump from MainActivity to SecondActivity to execute its function at runtime. Then, how can we transfer data between activities?
Sender:
Intent intent = new Intent (); intent. setclass (MainActivity. this, SecondActivity. class); intent. putExtra ("str", "hello"); // putExtra (); can send many types of data! StartActivity (intent );
Receiver:
Intent intent = getIntent (); Bundle bundle = intent. getExtras (); // What is bundle? Added in the comment. String str = bundle. getString ("str"); // The character is saved in str.
Then it is interesting. The above can be understood as: click a button to jump to another interface, and then you have to click a button in another interface to jump back and return a data.

Define in MainActivity:
Private final static int REQUEST_CODE = 1; // It is equivalent to a dedicated dark number and belongs to the intent defined below. If multiple Intent values are defined, different dark numbers need to be allocated. Why should we assign a dark number? It is for one-to-one to prevent the occurrence of many-to-one or one-to-many cases.
Button listening write:
Intent intent = new Intent (); intent. setclass (MainActivity. this, SecondActivity. class); intent. putExtra ("str", "hello"); // startActivity (intent); startActivityForResult (intent, REQUEST_CODE); // startActivity (intent); no return value, there is. but write a // onActivityResult method as follows:
Protected void onActivityResult (int requestCode, int resultCode, Intent data) // you can use the method constructor to construct it! {If (requestCode = REQUEST_CODE) // prevents multiple-to-one (if (resultCode = secondActivity. RESULT_CODE) // prevents one-to-multiple {Bundle bundle = data. getExtras (); String str = bundle. getString ("back"); Toast. makeText (MainActivity. this, str, Toast. LENGTH_LONG ). show ();}}}
In SecondActivity, define:
public final static int RESULT_CODE=1;
Button listening write:
Intent intent = new Intemt (); intent. putExtra ("back", "Back Data"); setResult (RESULT_CODE, intent); // set the return value to finish (); // kill the current activity
This is OK! Think about it. Is it easy for Android software?

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.