Intent means "intention, purpose", as the information transmission medium between different components, mainly the following three:
- Activity:startactivity () and Startactivityforresult ();
- Service:startservice () and Bindservice ();
- Broadcast:sendbroadcast (), Sendoderedbroadcast () and Sendstickybroadcast ();
Intent transfer parameters in different component parts, mainly related to the following categories:
- Basic data, such as strings, integers, and so on, are as follows:
// Send Intent.putextra ("Key1", "value1"); Intent.putextra ("Key2", 2.0f); // Accept Intent Intent This . getintent (); String value1 = Intent.getstringextra ("Key1"); float value2 = Intent.getfloatextra ("Key2", 0);
- Using bundles
- Integrating objects into the Serializable interface
Public class Implements Serializable {//can also be parcelable, similar
-
- Add bundles to the intent as follows:
// Send New Bundle (); New User (); User.setusername ("Fredric"); User.setpassword ("Sinny"); Bundle.putserializable ( "User", user); Intent.putextras (bundle); // Receive This = Intent.getextras (); = (user) bundle.get ("User");
- Coercion type conversions, as in the following example:
//SendList<user> userlist =NewArraylist<user>(); for(inti = 0; I < 10; i++) {User tmp=NewUser (); Tmp.setusername ("Sinny"); Tmp.setpassword ("Fredric" +i); Userlist.add (TMP);} Intent.putextra ("UserList", (Serializable) userlist);//ReceiveList<user> userlist = (list<user>) Intent.getserializableextra ("UserList"); Iterator<User> iterator =Userlist.iterator (); while(Iterator.hasnext ()) {User tmp=Iterator.next (); LOG.I (Tag_activity, Tmp.getusername ()+Tmp.getpassword ());}
Intent (1, passing parameters)