布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.msgSend.MainActivity"tools:ignore="MergeRootFrame" ><TextViewandroid:id="@+id/tv_pleaseInputPhoneNum"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/pleaseInputPhoneNum" /><EditTextandroid:id="@+id/et_phoneNum"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_below="@+id/tv_pleaseInputPhoneNum"android:layout_marginTop="14dp"android:ems="10"android:inputType="phone" /><TextViewandroid:id="@+id/et_pleaseInputContent"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_below="@+id/et_phoneNum"android:layout_marginTop="16dp"android:text="@string/pleaseinputcontent" /><EditTextandroid:id="@+id/et_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/et_pleaseInputContent"android:layout_marginTop="26dp"android:layout_marginRight="10dp"android:layout_marginLeft="10dp"android:ems="10"android:lines="5" ></EditText><Buttonandroid:id="@+id/bt_send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/et_content"android:layout_below="@+id/et_content"android:layout_marginTop="26dp"android:text="@string/send" /></RelativeLayout>
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;@Overrideprotected 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);}@Overridepublic 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();//短信是有长度限制的, 直接对内容进行分割List<String> contents = smsManager.divideMessage(content);//发送for (String content1 : contents) {smsManager.sendTextMessage(phoneNum, null, content1, null, null);}}}
效果: