发送短信之分割短信 SMSManager

package com.test.smsmanagerdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

/**
 * 发送短信实例
 */
public class SendSmsActivity extends AppCompatActivity {

EditText phone,content;
    Button send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_sms);

        //获取 SMSManager 管理器
        final SmsManager smsManager = SmsManager.getDefault();

        //初始化控件
        phone = (EditText) findViewById(R.id.et_phone);
        content = (EditText) findViewById(R.id.et_content);
        send = (Button) findViewById(R.id.btn_send);

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone_number = phone.getText().toString().trim();
                String sms_content = content.getText().toString().trim();
//                //创建一个 android.app.PendingIntent 对象
//                PendingIntent pi = PendingIntent.getActivity(SendSmsActivity.this,0,new Intent(),0);
//
//                //发送短信
//                smsManager.sendTextMessage(phone.getText().toString(),null,content.getText().toString(),
//                        pi,null);
//
//                //提示短信发送完成
//                Toast.makeText(SendSmsActivity.this, "短信发送完成", Toast.LENGTH_SHORT).show();

                if(phone_number.equals("")) {
                    Toast.makeText(SendSmsActivity.this, "收件人号码不能为空", Toast.LENGTH_LONG).show();
                } else {
                    SmsManager smsManager = SmsManager.getDefault();
                    if(sms_content.length() > 70) {
                        List contents = smsManager.divideMessage(sms_content);
                        for(String sms : contents) {
                            smsManager.sendTextMessage(phone_number, null, sms, null, null);
                        }
                    } else {
                        smsManager.sendTextMessage(phone_number, null, sms_content, null, null);
                    }
                    Toast.makeText(SendSmsActivity.this, "短信发送成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

你可能感兴趣的:(Android)