Intent intended for data transfer between activity

Source: Internet
Author: User

Transferred from: http://blog.sina.com.cn/s/blog_83940dfb0100veas.html

data transfer between activity in the start activity, sending dataprotected voidonCreate (Bundle saveinstancestate) {Super. OnCreate (saveinstancestate);          Setcontentview (r.layout.thisactivity); Intent Intent=NewIntent (); //set the start activity and target activity to indicate that the data is transferred from this activity to the next activityIntent.setclass (thisactivity. This, Targetactivity.class); //binding DataIntent.putextra ("username", username);//You can also bind an arrayIntent.putextra ("Userpass", Userpass); //Open target Activitystartactivity (Intent); In the target activity, the data is received:protected voidonCreate (Bundle saveinstancestate) {Super. OnCreate (saveinstancestate);               Setcontentview (r.layout.targetactivity); //Get IntentIntent Intent =getintent (); //reading DataString name = Intent.getstringextra ("username"); String Pass= Intent.getstringextra ("Userpass");you can also apply bundles (bundles) in the start activity to send data:protected voidonCreate (Bundle saveinstancestate) {Super. OnCreate (saveinstancestate);               Setcontentview (r.layout.thisactivity); Intent Intent=NewIntent (); //set the start activity and target activity to indicate that the data is transferred from this activity to the next activityIntent.setclass (thisactivity. This, Targetactivity.class); //bind multiple data at onceBundle bundle =NewBundle (); Bundle.putstring ("Username", username); Bundle.putstring ("Userpass", Userpass);          Intent.putextras (bundle); //Open target Activitystartactivity (Intent); In the target activity, the data is received:protected voidonCreate (Bundle saveinstancestate) {Super. OnCreate (saveinstancestate);               Setcontentview (r.layout.targetactivity); //Get IntentIntent Intent =getintent (); //reading DataBundle bundle =Intent.getextras (); String name= Bundle.getstring ("username"); String Pass= Bundle.getstring ("Userpass"); When you need to return data from the target activity to the original activity, you can use the above method to define a new Intent to pass the data, or you can use Startactivityforresult (Intent Intent,intRequestcode); method. In the start activity, send the data:protected voidonCreate (Bundle saveinstancestate) {Super. OnCreate (saveinstancestate);               Setcontentview (r.layout.thisactivity); Intent Intent=NewIntent (); //set the start activity and target activity to indicate that the data is transferred from this activity to the next activityIntent.setclass (thisactivity. This, Targetactivity.class); //binding DataIntent.putextra ("username", username);//You can also bind an arrayIntent.putextra ("Userpass", Userpass); //Open target ActivityStartactivityforresult (intent,1); }      //you need to override the Onactivityresult method      protected voidOnactivityresult (intRequestcode,intResultCode, Intent Intent) {         Super. Onactivityresult (requestcode,resultcode,intent); //determine if the result code is the same as the result code of the callback         if(ResultCode = = 1){            //Get backhaul dataString name = Intent.getstringextra ("name"); String Pass= Intent.getstringextra ("Pass");//working with Data            ......         } In the target activity, receive the data:protected voidonCreate (Bundle saveinstancestate) {Super. OnCreate (saveinstancestate);               Setcontentview (r.layout.targetactivity); //Get IntentIntent Intent =getintent (); //reading DataString name = Intent.getstringextra ("username"); String Pass= Intent.getstringextra ("Userpass");//get new data from EditText to name and passName =Edittext1.gettext (). toString (); Pass=Edittext2.gettext (). toString ()//data changes, you need to pass the changed value back to the original activityIntent.putextra ("name", name); Intent.putextra ("Pass", pass); //setresult (int resultcode,intent Intent) methodSetresult (1, intent); //Destroying this activity will automatically return to the previous activity when the activity is destroyed.finish (); }

Here are some other examples of intent usages that are reproduced (go from Javaeye) to display Web pages1. Uri uri = uri.parse ("http://google.com"); 2. Intent it =NewIntent (Intent.action_view, URI); 3. StartActivity (it); Show Map1. Uri uri = uri.parse ("geo:38.899533,-77.036476"); 2. Intent it =NewIntent (Intent.action_view, URI); 3. StartActivity (IT); 4.//other geo URI examples5.//Geo:latitude,longitude6.//Geo:latitude,longitude?z=zoom7.//geo:0,0?q=my+street+address8.//geo:0,0?q=business+near+city9.//Google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapzoomPath Planning1. Uri uri = uri.parse ("Http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng &hl=en "); 2. Intent it =NewIntent (Intent.action_view, URI); 3. StartActivity (IT); 4.//where Startlat, STARTLNG, Endlat, ENDLNG is a long with 6 decimals like:50.123456Call1.//call out Dialer2. Uri uri = uri.parse ("tel:0800000123"); 3. Intent it =NewIntent (intent.action_dial, URI); 4. StartActivity (IT); 1.//just call out .2. Uri uri = uri.parse ("tel:0800000123"); 3. Intent it =NewIntent (Intent.action_call, URI); 4. StartActivity (IT); 5.//with this, to be in Androidmanifest.xml, plus6.//<uses-permission id= "Android.permission.CALL_PHONE"/>Transferring SMS/MMS1.//Call SMS Program2. Intent it =NewIntent (Intent.action_view, URI); 3. It.putextra ("Sms_body", "The SMS Text")); 4. It.settype ("Vnd.android-dir/mms-sms")); 5. StartActivity (IT); 1.//Send Message2. Uri uri = uri.parse ("smsto://0800000123"); 3. Intent it =NewIntent (Intent.action_sendto, URI); 4. It.putextra ("Sms_body", "The SMS Text")); 5. StartActivity (IT); 1.//Transfer MMS2. Uri uri = uri.parse ("CONTENT://MEDIA/EXTERNAL/IMAGES/MEDIA/23"); 3. Intent it =NewIntent (intent.action_send); 4. It.putextra ("Sms_body", "some text"); 5. It.putextra (Intent.extra_stream, URI); 6. It.settype ("Image/png")); 7. StartActivity (it); Send email1. Uri uri = uri.parse ("Mailto:[email protected]"); 2. Intent it =NewIntent (Intent.action_sendto, URI); 3. StartActivity (IT); 1. Intent it =NewIntent (intent.action_send); 2. It.putextra (Intent.extra_email, "[EMAIL protected]"); 3. It.putextra (Intent.extra_text, "The email body TEXT"); 4. It.settype ("Text/plain")); 5. StartActivity (Intent.createchooser (it, "Choose Email Client"))); 1. Intent it=NewIntent (intent.action_send); 2. string[] tos={"[Email protected]"}; 3. string[] ccs={"[Email protected]"}; 4. It.putextra (Intent.extra_email, TOS); 5. It.putextra (INTENT.EXTRA_CC, CCS); 6. It.putextra (Intent.extra_text, "The email body TEXT"); 7. It.putextra (Intent.extra_subject, "The email SUBJECT text"); 8. It.settype ("message/rfc822")); 9. StartActivity (Intent.createchooser (it, "Choose Email Client"))); 1.//Transfer Attachments2. Intent it =NewIntent (intent.action_send); 3. It.putextra (Intent.extra_subject, "The email SUBJECT text"); 4. It.putextra (Intent.extra_stream, "File:///sdcard/mysong.mp3"); 5. Sendintent.settype ("Audio/mp3")); 6. StartActivity (Intent.createchooser (it, "Choose Email Client")) ); Play multimedia URI Uri= Uri.parse ("File:///sdcard/song.mp3"); Intent it=NewIntent (Intent.action_view, URI); It.settype ("Audio/mp3");        StartActivity (IT); Uri URI= Uri.withappendedpath (MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); Intent it=NewIntent (Intent.action_view, URI); StartActivity (IT); Market related1.//Find an app2. Uri uri = uri.parse ("Market://search?q=pname:pkg_name"); 3. Intent it =NewIntent (Intent.action_view, URI); 4.  StartActivity (IT); 5.//where Pkg_name is the full package path for an application1.//Show information about an app2. Uri uri = uri.parse ("market://details?id=app_id"); 3. Intent it =NewIntent (Intent.action_view, URI);4.  StartActivity (IT); 5.//where app_id is the application ID, find the ID6.//By clicking on your application in market home7.//page, and notice the ID from the address barUninstall Applications1. Uri uri = uri.fromparts ("package", Strpackagename,NULL); 2. Intent it =NewIntent (Intent.action_delete, URI); 3. StartActivity (it);

Intent intended for data transfer between activity

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.