建立布局 Activity_main.xml
<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/hello_world" />
<EditText
android:id="@+id/tel_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<EditText
android:id="@+id/sms_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/sms_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sms_btn" />
</LinearLayout>
values文件中strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SmsPhone</string>
<string name="action_settings">Settings</string>
<string name="hello_world">发送短信</string>
<string name="sms_btn">发送</string>
</resources>
MainActivity.java文件代码
在此处我们需要了解 Android中短信主要采用SmsManager的sendTextMessage()方法来发送文字短信。
sendTextMessage()方法有5个参数,第一个参数为对方的手机号码(不能为空),第二个参数为发送方的手机号号码(可以为空),第三个参数为发送的短信内容(不能为空),第四个参数为PendingIntent对象,用于判断发送短信是否成功(可以为空),第五个参数也为PendingIntent对象,当用户接收到短信时会返回该对象(可以为空)。
package com.example.smsphone;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.gsm.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText tel_num;
private EditText sms_text;
private Button sms_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tel_num= (EditText)findViewById(R.id.tel_num);
sms_text=(EditText)findViewById(R.id.sms_text);
sms_btn=(Button)findViewById(R.id.sms_btn);
sms_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String telNum=tel_num.getText().toString();
String msg=sms_text.getText().toString();
if (telNum==null) {
Toast.makeText(MainActivity.this, "号码不能为空", Toast.LENGTH_LONG).show();
}else {
if (msg.equals("")) {
Toast.makeText(MainActivity.this, "发送内容为空", Toast.LENGTH_LONG).show();
}else {
//获取默认的SMS管理器
SmsManager sms=SmsManager.getDefault();
PendingIntent pIntent=PendingIntent.getActivity(MainActivity.this, 0, new Intent(), 0);
//发送文本信息
sms.sendTextMessage(telNum, null, msg, pIntent, null);
}
}
}
});
}
@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;
}
}
运行结果:
@author www.lelexie.com