线性布局:发送短信

把信息发送到另一台模拟器
在这里插入图片描述
主布局资源文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:id="@+id/tv_input_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/input_phone"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/edt_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/input_phone"
        android:inputType="phone" />

    <TextView
        android:id="@+id/tv_input_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/input_message"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/edt_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/input_message"
        android:inputType="textMultiLine"
        android:lines="6" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_send"
            android:layout_width="96dp"
            android:layout_height="wrap_content"
            android:onClick="doSend"
            android:text="@string/send" />

        <Button
            android:id="@+id/btn_clear"
            android:layout_width="92dp"
            android:layout_height="wrap_content"
            android:onClick="doClear"
            android:text="@string/clear" />

        <Button
            android:id="@+id/btn_back"
            android:layout_width="96dp"
            android:layout_height="wrap_content"
            android:onClick="doBack"
            android:text="@string/back" />
    </LinearLayout>

</LinearLayout>

主界面类MainActivity

package com.example.myapplication03;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import java.util.List;

public class MainActivity extends Activity {
    private EditText edtPhone;
    private EditText edtMessage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 利用布局资源文件设置用户界面
        setContentView(R.layout.activity_main);

        // 通过资源标识获得控件实例
        edtPhone = (EditText) findViewById(R.id.edt_phone);
        edtMessage = (EditText) findViewById(R.id.edt_message);
    }

    /**
     * 发送按钮单击事件处理方法
     */
    public void doSend(View view) {
        //获取用户输入的电话号码
        String strPhone = edtPhone.getText().toString().trim();
        //获取用户输入的短信内容
        String strMessage = edtMessage.getText().toString().trim();
        //获取缺省的短信管理器对象
        SmsManager smsManager = SmsManager.getDefault();
        //创建发送短信的意图
        PendingIntent sentIntent = PendingIntent.getBroadcast(this,0,new Intent(),0);
        //根据短信长度决定是否要分条发送
        if(strMessage.length()>120){
            List<String> messageList = smsManager.divideMessage(strMessage);
            for (String message : messageList) {
                /**
                 * sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);
                 * 参数1:destinationAddress 对方手机号码
                 * 参数2:scAddress 短信中心号码,一般设置为空
                 * 参数3:text 短信内容
                 * 参数4:sentIntent 判断短信是否发送成功,
                 *      如果你没有SIM卡,或者网络中断,则可以通过这个intent来判断。
                 *      注意强调的是“发送”动作是否成功。至于对于对方是否收到,另当别论。
                 * 参数5:deliveryIntent 当短信发送到收件人时,会收到这个deliveryIntent。即强调了“发送”后的结果。
                 */
                smsManager.sendTextMessage(strPhone, null, message, sentIntent, null);
            }
        }else{
            //利用短信管理器的相应方法发送一条短信
            smsManager.sendTextMessage(strPhone,null,strMessage,sentIntent,null);
        }
    }

    /**
     * 清除按钮单击事件处理方法
     */
    public void doClear(View view) {
        // 清除电话号码编辑框
        edtPhone.setText("");
        // 清除短信编辑框
        edtMessage.setText("");
        // 让电话号码编辑框获得焦点
        edtPhone.requestFocus();
    }

    /**
     *  返回按钮单击事件处理方法
     */
    public void doBack(View view) {
        finish();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43127300/article/details/83820504