1、main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="手机号码" /> <EditText android:id="@+id/numET" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="phone" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="短信内容" /> <EditText android:id="@+id/contentET" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textMultiLine" android:lines="3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="hello" android:text="发送短信" /> </LinearLayout>
2、MainActivity
package com.example.smstest; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.telephony.SmsManager; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_num; private EditText et_content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et_num = (EditText) findViewById(R.id.numET); et_content = (EditText) findViewById(R.id.contentET); } public void hello(View view){ String num = et_num.getText().toString(); String content = et_content.getText().toString(); /** * 以下是发短信的核心代码 */ SmsManager smsManager = SmsManager.getDefault(); List<String> list = smsManager.divideMessage(content);//分割短信内容.用于处理一条短信太长的情况... for(String str : list){ smsManager.sendTextMessage(num, null, str, null, null); } Toast.makeText(this, "hello sms", 1).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
3、AndroidManifest.xml
<uses-permission android:name="android.permission.SEND_SMS"/>
源码下载:http://download.csdn.net/detail/caihongshijie6/7600765