在主activityMain中写如下代码:
package com.zzuli.message;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View v){
EditText et_phone=(EditText) findViewById(R.id.et_phone);
EditText et_message=(EditText) findViewById(R.id.et_message);
//获得电话号码
String phone = et_phone.getText().toString();
String messagecontext=et_message.getText().toString();
//获得短信管理器
SmsManager send=SmsManager.getDefault();
//切割短信,把长短信分成若干个小短信
ArrayList<String> sms=send.divideMessage(messagecontext);
for(String string:sms){
//发送短信
send.sendTextMessage(phone, null, string, null, null);
}
}
}
在layout布局文件中的代码为
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.zzuli.message.MainActivity" >
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:hint="请输入短信号码"
/>
<EditText
android:id="@+id/et_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="5"
android:hint="请输入短信内容"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"
android:onClick="send"
/>
</LinearLayout>
这样就可以得到一个简单的短信发送器了