Startactivity, startactivityforresult, broadcast usage

Source: Internet
Author: User

Preface

I have been busy with projects recently and haven't written any text for a long time. Today, a group of children's shoes asked a question about passing values between different interfaces. In this background, I wrote a text about "startactivity, startactivityforresult, and broadcast use, I hope it will be helpful for new users.

 

① Startactivity and startactivityforresult:

 Intent intent;
@ Override public void onclick (view v) {int tag = v. GETID (); Switch (TAG) {// use startactivityforresult case R. id. btntzbystartactivtyforresult: intent = new intent (mainactivity. this, modifyactivity. class); startactivityforresult (intent, 0); break; // use broadcast case R. id. btntzbystartactivty: intent = new intent (mainactivity. this, modifyactivity. class); startactivity (intent); break; default: break ;}}

② Use of broadcast:

First, define a broadcast receiving class:

// Define a broadcastreceiver broadcast receiving Class: public class receivebroadcast extends broadcastreceiver {@ override public void onreceive (context, intent data) {string actionname = data. getaction (); If (myactionname. equals (actionname) {// obtain the data from the broadcast and display bundle extras = data. getextras (); If (extras! = NULL) {string [] value = extras. getstringarray ("data"); tv1.settext (value [0]); tv2.settext (value [1]); tv3.settext (value [2]); tv4.settext (value [3]) ;}}}

Second, register broadcast:

// Register public void registerboradcastreceiver () {receivebroadcast = new receivebroadcast (); intentfilter filter = new intentfilter (); filter. addaction (myactionname); // only the recipient holding the same action can receive this broadcast registerreceiver (receivebroadcast, filter );}

Again, the broadcast is sent:

String [] value2 = new string [] {"111", "222", "333", "444"}; intent tent2 = new intent ("myactiontag "); // broadcast tags must be the same as those accepted. Tent2.putextra ("data", value2); sendbroadcast (tent2); // send broadcast this. Finish ();

What do not say, on the demo: http://download.csdn.net/detail/xiaojia7283/8099919 welcome to download.

Startactivity, startactivityforresult, broadcast usage

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.