布局:
activity代码:
package com.example.msgSend;import java.util.List;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;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;public class MainActivity extends ActionBarActivity implements OnClickListener{ /** 电话号码 */ private EditText etPhoneNum; /** 短信内容 */ private EditText etContent; /** 发送按钮 */ private Button btSend; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获得组件 etPhoneNum = (EditText)findViewById(R.id.et_phoneNum); etContent = (EditText)findViewById(R.id.et_content); btSend = (Button)findViewById(R.id.bt_send); System.out.println("获取成功"); //注册点击事件 btSend.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_send: sendMessage(); break; default: break; } } //实现短信发送 private void sendMessage() { String phoneNum = etPhoneNum.getText().toString(); String content = etContent.getText().toString(); //吐司提示 if (TextUtils.isEmpty(phoneNum) || TextUtils.isEmpty(content)) { Toast.makeText(this, "手机号码和短信都不能为空", Toast.LENGTH_LONG).show(); return ; } SmsManager smsManager = SmsManager.getDefault(); //短信是有长度限制的, 直接对内容进行分割 Listcontents = smsManager.divideMessage(content); //发送 for (String content1 : contents) { smsManager.sendTextMessage(phoneNum, null, content1, null, null); } }}
效果: