Android Development Learning: calling and texting

Source: Internet
Author: User

1. Create a new Android project

File--new--other--android Application Project

Fill in the application name (which is the name of the app, such as: every day cool run)

Fill in Project name (that is, the program project name, for example: TTKP, the name is Ttkp.app after packaging)

Fill in the Package name (program name, e.g. CN.TENGXUN.TTKP)

Then choose the minimum running Android version, the most suitable version, the compiled version, the theme.

next--next--Select your Android app icon picture and then finish.


Then we have to focus on res (put the resource file, the static text can be written on the inside)

SRC Code Programming file

Gen (automatically generated resource ID generation file)

Androidmanifest.xml is the application configuration file

Layout under res is a configuration file for layouts


2. Write a call function

Write the layout configuration file First Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "     android:orientation=" vertical "      android:layout_width=" match_parent "     android:layout_height= "Match_parent" >    <textview        android:layout_width= "match_parent"        android:layout_height= "Wrap_ Content "        android:text=" @string/phone_title "/>    <edittext         android:layout_width=" Match_parent "        android:layout_height= "wrap_content"          android:hint= "@string/phone_title"        android:id= "@+id/telnum "/>    <button         android:layout_width=" wrap_content "        android:layout_height=" Wrap_content "        android:text= "@string/phone_button"         android:id= "@+id/button"/></linearlayout>

@ means to manipulate gen R.java file information, get it; @+ is created.


Write a text message

String.xml under the value below Res

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <string name= "app_name" > Dialer </string >    <string name= "Hello_world" >hello world!</string>    <string name= "Action_settings" > settings</string>    <string name= "Phone_title" > Please enter phone number </string>    <string name= "Phone_ Button "> Dialing </string></resources>

Writing Dialing Event codes

Java code under SRC

public class Mainactivity extends Activity {    private EditText EditText;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        edittext= (EditText) Findviewbyid (r.id.telnum);        Button button= (button) This.findviewbyid (R.id.button);        Button.setonclicklistener (New Buttonclicklistener ());    }        Private Final class Buttonclicklistener implements view.onclicklistener{@Overridepublic void OnClick (View v) {//TODO Aut O-generated method Stubstring Telnum=edittext.gettext (). toString (); Intent intent=new Intent (); Intent.setaction (" Android.intent.action.CALL "); Intent.setdata (Uri.parse (" Tel: "+telnum)); StartActivity (intent);}}        

Finally you want to get permission to call this feature on your Android phone

Applying Profiles in Androidmanifest.xml

<uses-permission android:name= "Android.permission.CALL_PHONE"/>

3. Write SMS function

Layout configuration file

   <edittext         android:layout_width= "match_parent"        android:layout_height= "wrap_content"          android:hint = "@string/phone_title"        android:id= "@+id/telnum"/>    <edittext         android:layout_width= "Match_ Parent "        android:layout_height=" wrap_content "        android:minlines=" 3 "        android:hint=" @null "        Android:id= "@+id/message"                 />       <button         android:layout_width= "Match_parent"        android: layout_height= "Wrap_content"        android:text= "@string/message_button"         android:id= "@+id/message_button"/ >

String configuration file

<string name= "Message_button" > Send SMS </string>

Java code

public class Mainactivity extends Activity {private EditText phonetext;private EditText EditText;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        phonetext= (EditText) Findviewbyid (r.id.telnum); edittext= (EditText) Findviewbyid (r.id.message);        Button message_button= (button) This.findviewbyid (R.id.message_button);            Message_button.setonclicklistener (New Messagebuttonclicklistener ()); } Private Final class Messagebuttonclicklistener implements view.onclicklistener{@Overridepublic void OnClick (View v) { Texting string Phonenumber=phonetext.gettext (). toString (); String Message=edittext.gettext (). toString (); Smsmanager Manager=smsmanager.getdefault (); arraylist<string> messages = manager.dividemessage (message); for (String content:messages) {    Manager.sendtextmessage (PhoneNumber, NULL, content, NULL, NULL); } toast.maketext (Mainactivity.this, R.strIng.success, Toast.length_long). Show ();              Write SMS record contentvalues values = new Contentvalues ();              Values.put ("Address", PhoneNumber);              Values.put ("Body", message);              Values.put ("type", "2");                  Values.put ("read", "1");//1 indicates read getcontentresolver (). Insert (Uri.parse ("Content://sms/inbox"), values);   }        }   }

Join permissions

    <uses-permission android:name= "Android.permission.SEND_SMS"/>    <uses-permission android:name= " Android.permission.WRITE_SMS "/>      <uses-permission android:name=" Android.permission.READ_SMS "/>


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Development Learning: calling and texting

Related Article

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.