效果图:
manifest.xml 文件中加入 <uses-permission android:name="android.permission.SEND_SMS"/>
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">sms</string>
<string name="action_settings">Settings</string>
<string name="number">请输入手机号</string>
<string name="content">请输入文本</string>
<string name="button">发送</string>
<string name="success">短信发送成功!</string>
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/number" /> <EditText
android:id="@+id/numberText"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content" /> <EditText
android:id="@+id/contentText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="3" /> <Button
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button" /> </LinearLayout>
package jk.sms; import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
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 Activity { private EditText NumberText;
private EditText ContentText; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NumberText = (EditText) this.findViewById(R.id.numberText);
ContentText = (EditText) this.findViewById(R.id.contentText);
Button button = (Button) this.findViewById(R.id.Button);
button.setOnClickListener(new ButtonClickListener());
}
@SuppressLint("ShowToast")
private final class ButtonClickListener implements OnClickListener { public void onClick(View v) {
String number = NumberText.getText().toString();
String content = ContentText.getText().toString();
SmsManager manager = SmsManager.getDefault();
ArrayList<String> texts = manager.divideMessage(content); //超过规定长度后短信拆分
for (int i = 0; i < texts.size(); i++) {
manager.sendTextMessage(number, null, content, null, null);
}
Toast.makeText(MainActivity.this, R.string.success,
Toast.LENGTH_LONG);
}
} 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;
} }