android基础知识点复习之短信发送

界面布局:

activity_main.xml

<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"

    tools:context=".MainActivity" >



    <TextView

        android:id="@+id/phonenumber_text"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="@string/phonenumber" />



    <EditText

        android:id="@+id/phonenumber_edit"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/phonenumber_text"

        android:inputType="phone" >

    </EditText>



    <TextView

        android:id="@+id/phonenumber_text2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/phonenumber_edit"

        android:text="@string/phonenumber" />



    <EditText

        android:id="@+id/message_edit"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/phonenumber_text2"

        android:lines="5" />



    <Button

        android:id="@+id/call_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_below="@id/message_edit"

        android:onClick="callphone"

        android:text="call" />



</RelativeLayout>

java代码:

import android.annotation.SuppressLint;

import android.app.Activity;

import android.os.Bundle;

import android.telephony.SmsManager;

import android.text.TextUtils;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;



import java.util.ArrayList;



public class SendMessage extends Activity implements OnClickListener {



    private EditText phonenumber_edit;

    private EditText content_eidt;

    private Button Send_button;

    private String content ;

    private  String phoneNumber;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        initViews();

    }

    /**

     * 

     * @Title: initViews 

     * @Description: TODO(初始化布局文件) 

     * @param     设定文件 

     * @return void    返回类型 

     * @throws

     */

    private void initViews() {

        phonenumber_edit = (EditText)findViewById(R.id.phonenumber_edit);

        content_eidt = (EditText)findViewById(R.id.message_edit);

        Send_button = (Button)findViewById(R.id.call_button);

        Send_button.setOnClickListener(this);

    }

    /**

     * 

     * @Name onClick

     * @Description TODO(按钮单击事件处理) 

     * @param v

     * @see android.view.View.OnClickListener#onClick(android.view.View)

     * @Date 2013-12-8 下午10:28:30

     *

     */

    @SuppressLint("NewApi")

    @Override

    public void onClick(View v) {

        switch (v.getId()) {

            //打你发送短信按钮

            case R.id.call_button:

                //取得短信内容

                content = content_eidt.getText().toString().trim();

                //取得手机号码

                phoneNumber = phonenumber_edit.getText().toString().trim();

                //判断字符是否为空

                if(TextUtils.isEmpty(content)||TextUtils.isEmpty(phoneNumber)){

                    Toast.makeText(getApplicationContext(), "请输入内容", Toast.LENGTH_LONG).show();

                    return;

                }

                //短信发送

                SmsManager smsManager = SmsManager.getDefault();

                ArrayList<String> contents = smsManager.divideMessage(content);

                for (String string : contents) {

                    smsManager.sendTextMessage(phoneNumber, null, string, null, null);

                }

                Toast.makeText(getApplicationContext(), "短信已经发送", Toast.LENGTH_LONG).show();

                break;



            default:

                break;

        }



    }

}

权限设置:

   <!-- 发送短信权限的权限 -->

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

 

你可能感兴趣的:(android)