Learn the android<intent step from scratch. 41 .>

Source: Internet
Author: User

In the previous section we learned about the basic usage of intent, the jumps between multiple activity and the passing of information.

Today we use Intnet to complete some special operations, such as making calls, sending text messages, browsing the web, etc...


OK nonsense not much to say, directly on the tutorial, specifically in the code comments specifically explained

1. Open a Web page using intent


<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <textview android:id=" @+id/textview1 "android:layout_width=" Wrap_content "an droid:layout_height= "Wrap_content" android:layout_alignparenttop= "true" Android:layout_centerhorizontal= "Tru E "android:layout_margintop=" 20DP "android:text=" Please enter the page you want to visit "android:textappearance="? Android:attr/te        Xtappearancelarge "/> <edittext android:id=" @+id/edittext1 "android:layout_width=" Wrap_content " Android:layout_height= "Wrap_content" Androidoid:layout_below= "@+id/textview1" android:layout_centerhorizontal= "true" android:layout_margintop= "18DP" Android:ems= "Ten" > </EditText> <button android:id= "@+id/button1" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_below= "@+id/edittext1" Android:lay Out_centerhorizontal= "true" android:layout_margintop= "29DP" android:text= "Access"/></relativelayout>


Java files

Package Com.example.intent4;import Android.app.activity;import Android.content.intent;import Android.net.Uri; Import Android.os.bundle;import android.view.view;import android.widget.button;import android.widget.EditText;    Import Android.widget.toast;public class Mainactivity extends Activity {private Button button;private EditText urlinfo;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);   Super.setcontentview (R.layout.activity_main);   button= (Button) This.findviewbyid (R.id.button1);   urlinfo= (EditText) This.findviewbyid (R.ID.EDITTEXT1); Button.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated Method Stubstring Url=urlinfo.gettext (). toString (). Trim (), if (! "". Equals (URL)) {//secure authentication of input content Intent intent=new intent (); Uri Uri=uri.parse ("http://" +url);//Set URI data intent.setaction (Intent.action_view);//Set Intent to point to Intent.setdata (URI);// Set data MainActivity.this.startActivity (intent);//Start Intent}else {toast.maketext (mainactivity.this, "The content you entered is empty, cannot be accessed", 2). Show ();}}); }    }



After clicking the button


2. Make a call using intent


<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <button android:id=" @+id/button1 "android:layout_width=" Wrap_content "Androi        d:layout_height= "Wrap_content" android:layout_below= "@+id/edittext1" android:layout_centerhorizontal= "true"        android:layout_margintop= "44DP" android:text= "dial"/> <textview android:id= "@+id/textview1" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_alignparentlef T= "true" android:layout_alignparenttop= "true"        android:text= "number" android:textappearance= "? Android:attr/textappearancelarge"/> <edittext A Ndroid:id= "@+id/edittext1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a Ndroid:layout_alignparentright= "true" android:layout_aligntop= "@+id/textview1" android:layout_marginright= "1 6DP "android:ems=" > <requestfocus/> </EditText></RelativeLayout>


Package Com.example.intent5;import Android.net.uri;import Android.os.bundle;import android.app.activity;import Android.content.intent;import Android.view.menu;import Android.view.view;import Android.widget.Button;import Android.widget.edittext;import Android.widget.toast;public class Mainactivity extends Activity {private button button;    Private EditText Phoneinfo;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);   Setcontentview (R.layout.activity_main);   button= (Button) This.findviewbyid (R.id.button1);   phoneinfo= (EditText) This.findviewbyid (R.ID.EDITTEXT1); Button.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method stubif (! ("". Equals (Phoneinfo.gettext (). ToString ())) {String phone=phoneinfo.gettext (). ToString (); Intent Intent =new Intent (); URI Uri=uri.parse ("Tel:" +phone); Intent.setdata (URI); intent.setaction (Intent.action_call); MainActivity.this.startActivity (intent);}    else {toast.maketext (mainactivity.this, "phone number is empty", 2). Show ();}}); }    }






3. Send short interest using intent


<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <textview android:id=" @+id/textview1 "android:layout_width=" Wrap_content "an droid:layout_height= "Wrap_content" android:layout_alignparentleft= "true" android:text= "number" Android:te Xtsize= "20px"/> <edittext android:id= "@+id/edittext1" android:layout_width= "Wrap_content" a ndroid:layout_height= "Wrap_content" android:layout_alignparentright= "true" android:layout_alignparenttop= "tr     UE "android:ems="/> <button   Android:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_alignparentbottom= "true" android:layout_centerhorizontal= "true" android:text= "Send"/> & Lt EditText android:id= "@+id/edittext2" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "android:layout_alignparentleft=" true "android:layout_alignright=" @+id/edittext2 "Android:lay        Out_centervertical= "true" android:ems= "ten" android:gravity= "Top" android:inputtype= "Textmultiline" android:lines= "> <requestfocus/> </EditText></RelativeLayout>

Java files

Package Com.example.intent6;import Android.net.uri;import Android.os.bundle;import android.app.activity;import Android.content.intent;import Android.view.menu;import Android.view.view;import Android.widget.Button;import Android.widget.edittext;import Android.widget.toast;public class Mainactivity extends Activity {private button button; Private EditText number, content, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); button = (button) This.findviewbyid (R.id.button1); Number = (EditText) This.findviewbyid (r.id.edittext1); content = (EditText) This.findviewbyid (R.ID.EDITTEXT2); Button.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method Stubstring Numberinfo=number.gettext (). toString (); String Contentinfo=content.gettext (). toString (); Equals (numberinfo) | | "". Equals (ContentInfo)) {Intent Intent =new Intent (); Uri uri=uri.parse ("Smsto:" +numbeRinfo);//Set the sending number Intent.settype ("vnd.android-dir/mms-sms");//Form Intent.putextra ("Sms_body", contentinfo);// Content Intent.setaction (Intent. ACTION_SENDTO);//Object Intent.setdata (URI);//Set Information MainActivity.this.startActivity (intent);} else {toast.maketext (mainactivity.this, "the number or content is empty, cannot be sent", 2). Show ();}});}}






Use intent to invoke multiple methods in the system to configure different requirements


Next forecast: Component communication Mesaage

Learn the android<intent step from scratch. 41 .>

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.