Android Development Notes Intent Primary learning course _android

Source: Internet
Author: User

This article describes the intent Beginner learning course for Android development notes. Share to everyone for your reference, specific as follows:

Project Creation steps:

New Android project->

Project name:intent

Build Target:android 2.2

Application Name:intentdemo

Package name:com.b510.intent.activity

Create activity:mainactivity

Min SDK Version:8

Finish

1. Dial the telephone

Press a button to start the call program

2. Edit Main.xml

<!--call button-->
<button
 android:id= "@+id/mainbtn"
 android:layout_width= "Wrap_content"
 android:layout_height= "Wrap_content"
 android:text= "@string/mainbtn"
/>

3. Edit Mainactivity.java File


   * * Define a MAINBTN variable
/private Button mainbtn=null;
public void OnCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.main);
    Locate the button
    mainbtn= (button) Findviewbyid (R.ID.MAINBTN) configured in the Main.xml file from the R.java file by ID;
    Set Button's listener
    Mainbtn.setonclicklistener (listener) when clicked;
  Private Onclicklistener listener=new Onclicklistener () {
    @Override public
    void OnClick (View v) {
      // Affirming a Intent
      Intent intent=new Intent ();
      Set ACTION to call
      intent.setaction (intent.action_call);
      Call 5554 cell phone, the number in front of "Tel:" is a required field
      Intent.setdata (Uri.parse ("tel:5554"));
      Start Activity
      startactivity (intent);}}


4. Edit Androidmanifest.xml File

</application>
   <!--to add permissions to allow phone calls-->
   <uses-permission android:name= " Android.permission.CALL_PHONE "/>

5, send SMS

Send a text message and call a phone like

5.1. Modify Mainactivity.java File

Private Onclicklistener listener=new Onclicklistener () {
    @Override public
    void OnClick (View v) {
      // Affirming a Intent
      Intent intent=new Intent ();
      Set ACTION to call
      intent.setaction (intent.action_call);
      Call 5554 cell phone, the number in front of "Tel:" is a required field
      Intent.setdata (Uri.parse ("tel:5554"));
      Start Activity
      startactivity (intent);
    }
;

5.2. Modify Androidmanifest.xml File

<!--to add permission to send SMS-->
   <uses-permission android:name= "Android.permission.SEND_SMS"/>

6. Start a new activity

Before talking about using intent to make a phone call and send a text message, the main introduction is the intent of some simple methods, here intent started another activity, in the previous content is in a process of operation. But in practical applications, we have a lot of activity, how to jump from one activity to another activity, then we need to use intent, since there are multiple activity, then we have to create another activity file, such as: Hongtenactivity.java

6.1. Edit Hongten.xml File

<textview 
 android:id= "@+id/hongtentext"
 android:layout_width= "fill_parent" 
 android:layout_ height= "Wrap_content" 
 android:text= "@string/hello"
/>
<!--jumps to another mainactivity-->
< Button
 android:id= "@+id/hongtenbtn"
 android:layout_width= "wrap_content"
 android:layout_height= " Wrap_content "
 android:text=" @string/hongtenbtn "
/>

6.2. Edit Hongtenactivity.java File

The public class Hongtenactivity extends activity {/* * defines a HONGTENBTN variable */private Button hongtenbtn = null;
  /* Define a HONGTENTEXT variable * * Private TextView hongtentext = null;
  Public final static int result_code = 1;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.hongten);
    First, the Intent Intent Intent = Getintent () is obtained from the Mainactivity class.
    In Mainactivity yonder is Setextras (Name,key)//Here is Getextras (), his return type is a Bundle type Bundle Bundle = Intent.getextras ();
    String Text = bundle.getstring ("str");
    Hongtentext = (TextView) Findviewbyid (R.id.hongtentext);
    Hongtentext.settext (text + "This is the parameter passed from the Mainactivity class");
    HONGTENBTN = (Button) Findviewbyid (R.ID.HONGTENBTN);
  Hongtenbtn.setonclicklistener (backmainactivity);
      Private Onclicklistener backmainactivity = new Onclicklistener () {@Override public void OnClick (View v) {
      Intent Intent = new Intent (); INtent.setclass (Hongtenactivity.this, Mainactivity.class);
      Intent.putextra ("Back", "Hello mainactivity");
      Start activity//startactivity (intent);
      Setresult (Result_code, intent); End the Hongtenactivity class, return to the Mainactivity class,//and pass the string named "Back" to the Mainactivity class//
    The Onactivityresult () method in the Mainactivity class is also called finish ();
}
  };

 }

6.3. Modify Androidmanifest.xml File

<activity android:name= ". Hongtenactivity "
  android:label=" @string/hongtenbtn ">  
</activity>

6.4. Edit Mainactivity.java File

* * Define a GOTOACTIVITYBTN variable/private Button gotoactivitybtn=null;
    Gotoactivitybtn= (Button) Findviewbyid (R.ID.GOTOACTIVITYBTN);
  Gotoactivitybtn.setonclicklistener (gotootheractivity); Private Onclicklistener gotootheractivity=new Onclicklistener () {@Override public void OnClick (View v) {//
      Affirming a Intent Intent intent=new Intent ();
      Jumps to the activity Intent.setclass named Hongtenactivity (Mainactivity.this, Hongtenactivity.class);
      Pass the "Hello hongtenactivity" string in the name "str" as a parameter to the Hongtenactivity class Intent.putextra ("Str", "Hello hongtenactivity");
      Start Activity//startactivity (intent);
    Startactivityforresult (Intent, Request_code);
  }
  }; /** * <li><b> Parameters:</b></li></br> *  

6.5. Edit String.xml File

<string name= "Hello" >hello world, mainactivity!</string>
<string name= "App_name" >IntentDemo </string>
<string name= "hongtenbtn" > Whereabouts mainactivity</string>
<string name= " Gotoactivitybtn "> Whereabouts hongtenactivity</string>
<string name=" Secondbtn ">second</string>
<string name= "Mainbtn" >Call</string>

For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", "Android Basic Components Usage Summary", " Android View tips Summary, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

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.