Android development and learning: Call and text message, android text message

Source: Internet
Author: User

Android development and learning: Call and text message, android text message

1. Create an android Project

File -- New -- Other -- android application project

Enter the application name (that is, the application name, for example, daily cool run)

Enter the project name (for example, TTKP)

Enter the package name (for example, cn. tengxun. ttkp)

Then select the minimum running android version, which is the most suitable version, compilation version, and topic.

NEXT--NEXT -- select your android app icon image, and then complete.


Then we will pay attention to the res (put the resource file, and the static text can be written in it)

Src code programming File

Gen (automatically generated resource ID file)

AndroidManifest. xml is the application configuration file.

Layout under res is the layout configuration file


2. Compile a call Function

First write the layout configuration file 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>

@ Indicates to operate the information of the R. java file under gen and obtain it; @ + indicates creation.


Compile text information

String. xml under the value under res

<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "app_name"> dial </string> <string name = "hello_world"> Hello world! </String> <string name = "action_settings"> Settings </string> <string name = "phone_title"> enter the mobile phone number </string> <string name = "phone_button"> dialing </string> </resources>

Write dialing Event code

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 Auto-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 need to obtain the permission to call android.

Apply the configuration file in AndroidManifest. xml

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

3. Compile the text message 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) {String phoneNumber = phonetext. getText (). toString (); String message = edittext. getText (). toString (); SmsManager manager = SmsManager. getDefault (); ArrayList <String> messages = manager. divideMessage (message); for (String content: messages) {// send a text message manager. sendTextMessage (phoneNumber, null, content, null, null); // write the 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);} Toast. makeText (MainActivity. this, R. string. success, Toast. LENGTH_LONG ). show ();}}}

Add permission

    <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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.