package com.tsh.sms;
import java.util.ArrayList;
android.app.Activity;
android.os.Bundle;
android.telephony.SmsManager;
android.text.TextUtils;
android.view.View;
android.view.View.OnClickListener;
android.widget.Button;
android.widget.EditText;
android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private EditText et_number;
EditText et_content;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_send=(Button) findViewById(R.id.bt_send);
et_number=(EditText) findViewById(R.id.et_number);
et_content=(EditText) findViewById(R.id.et_content);
bt_send.setOnClickListener(this);
}
@Override
onClick(View v) {
switch (v.getId()) {
case R.id.bt_send:
String number=et_number.getText().toString().trim();
String content=et_content.getText().toString().trim();
if(TextUtils.isEmpty(number)||TextUtils.isEmpty(content)){
Toast.makeText(this,"电话号码和内容都不能为空",Toast.LENGTH_SHORT).show();
return;
}
SmsManager smsManager=SmsManager.getDefault();
ArrayList<String> contents = smsManager.divideMessage(content);
for(String str:contents){
smsManager.sendTextMessage(number,null,str,null);
}
break;
}
}
}
<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
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight
android:paddingTop
tools:context="com.tsh.sms.MainActivity" >
TextView
android:id="@+id/tv_input_number"
android:layout_width="wrap_content"
android:layout_height
android:layout_alignParentLeft="true"
android:layout_alignParentTop
android:text="@string/input_tel_number" />
EditText
="@+id/et_number"
android:singleLine
android:layout_below="@id/tv_input_number"
android:ems="10"
android:inputType="phone" >
requestFocus />
</EditText="@+id/tv_number"="@id/et_number"="@string/sms_content" ="@+id/et_content"
android:lines="5"="@id/tv_number"="10" Button
="@+id/bt_send"="@id/et_content"="@string/send" />
RelativeLayout>