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