---
title: "Android基础教程之----SMS简单发送短信程序(两个模拟器之间的通信)!"
author: "mmdev"
date: "2009-12-15T23:02:00Z"
tags: []
slug: "android-sms"
draft: false
lastmod: "2009-12-15T23:02:00Z"
id: 990397
url: "https://mmdev.iteye.com/blog/990397"
---

前面的范例,示范了如何通过程序拨打电话,在**GSM**移动通信系统的服务中,除了打电话外,另一个常用的功能,就是发短信.也因为如此,许多电信业者推出专属短信族的专用费率,由此可知短信功能对手机的重要性.

发送短信的关键程序是通过**SmsManager**对象的**sendTextMessage()**方法来完成,其中**sendTextMessage()**方法需传入五个值,依次是收件人地址(**String**),发送地址(**String**),发送服务(**PendingIntent**)与送达服务(**PendingIntent**),其中收件人与正文是不可为**null**的两个参数.

本例子通过两个模拟器,**5554**,**5556**互相通信,下面我将分5个步骤,讲一下发送短信程序是如何实现的.

**Step 1**:建立一个**Android**工程,我们命名为**SMSDemo**.

**Step 2**:设计一下程序的**UI**,也就是主界面**main.xml**,这里用**AbsoluteLayout**,有点丑见笑了!代码如下:

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
**&lt;AbsoluteLayout**  
android:layout\_width="fill\_parent"  
android:layout\_height="fill\_parent"  
xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"  
&gt;  
**&lt;TextView**  
**android:layout\_width="wrap\_content"**  
**android:layout\_height="wrap\_content"**  
**android:text="收件 人:"**  
**android:textSize="16sp"**  
**android:layout\_x="0px"**  
**android:layout\_y="12px"**  
**&gt;**  
**&lt;/TextView&gt;**  
**&lt;EditText**  
**android:id="@+id/myEditText1"**  
**android:layout\_width="fill\_parent"**  
**android:layout\_height="wrap\_content"**  
**android:text=""**  
**android:textSize="18sp"**  
**android:layout\_x="60px"**  
**android:layout\_y="2px"**  
**&gt;**  
**&lt;/EditText&gt;**  
**&lt;EditText**  
**android:id="@+id/myEditText2"**  
**android:layout\_width="fill\_parent"**  
**android:layout\_height="223px"**  
**android:text=""**  
**android:textSize="18sp"**  
**android:layout\_x="0px"**  
**android:layout\_y="52px"**  
**&gt;**  
**&lt;/EditText&gt;**  
**&lt;Button**  
**android:id="@+id/myButton1"**  
**android:layout\_width="162px"**  
**android:layout\_height="wrap\_content"**  
**android:text="发送短信"**  
**android:layout\_x="80px"**  
**android:layout\_y="302px"**  
**&gt;**  
**&lt;/Button&gt;**  
**&lt;/AbsoluteLayout&gt;**

**Step 3:**主控制程序**SMSDemo.java**如下:

package com.android.test;

import android.app.Activity;  
**import android.app.PendingIntent;**  
**import android.content.Intent;**  
import android.os.Bundle;  
**import android.telephony.gsm.SmsManager;**  
**import android.view.View;**  
**import android.widget.Button;**  
**import android.widget.EditText;**  
**import android.widget.Toast;**

public class SMSDemo extends Activity {

**private Button mButton1;**  
**private EditText mEditText1;**  
**private EditText mEditText2;**  
public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
setContentView(R.layout.main);

**//获取资源**  
**mEditText1 = (EditText)findViewById(R.id.myEditText1);**  
**mEditText2 = (EditText)findViewById(R.id.myEditText2);**  
**mButton1 = (Button)findViewById(R.id.myButton1);**  
**//发送短信的响应**  
**mButton1.setOnClickListener(new Button.OnClickListener(){**

**public void onClick(View v) {**  
**//获取发送地址和发送内容**  
**String messageAddress = mEditText1.getText().toString();**  
**String messageContent = mEditText2.getText().toString();**

**//构建一取得default instance的SmsManager对象**

**SmsManager smsManager = SmsManager.getDefault();**  
**//检查输入内容是否为空,这里为了简单就没有判断是否是号码,短信内容长度的限制也没有做**  
**if(messageAddress.trim().length()!=0 && messageContent.trim().length()!=0)**  
**{**  
**try{**  
**PendingIntent pintent = PendingIntent.getBroadcast(SMSDemo.this, 0, new Intent(), 0);**  
**smsManager.sendTextMessage(messageAddress, null, messageContent, pintent, null);**

**}catch(Exception e)**  
**{**  
**e.printStackTrace();**  
**}**  
**//提示发送成功**  
**Toast.makeText(SMSDemo.this, "发送成功", Toast.LENGTH\_LONG).show();**  
**}**  
**else{**  
**Toast.makeText(SMSDemo.this, "发送地址或者内容不能为空", Toast.LENGTH\_SHORT).show();**  
**}**  
**}**

**});**  
}  
}

**Step 4:**增加拨打电话权限**AndroidManifest.xml**代码如下:

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;manifest xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"  
package="com.android.test"  
android:versionCode="1"  
android:versionName="1.0"&gt;  
&lt;application android:icon="@drawable/icon" android:label="@string/app\_name"&gt;  
&lt;activity android:name=".SMSDemo"  
android:label="@string/app\_name"&gt;  
&lt;intent-filter&gt;  
&lt;action android:name="android.intent.action.MAIN" /&gt;  
&lt;category android:name="android.intent.category.LAUNCHER" /&gt;  
&lt;/intent-filter&gt;  
&lt;/activity&gt;

&lt;/application&gt;  
&lt;uses-sdk android:minSdkVersion="3" /&gt;  
**&lt;uses-permission android:name="android.permission.SEND\_SMS"&gt;&lt;/uses-permission&gt;**  
&lt;/manifest&gt;

**Step 5:run it!**效果图如下,**5554**给**5556**发送了一条短信:

![](http://p.blog.csdn.net/images/p_blog_csdn_net/Android_Tutor/EntryImages/20091215/sms1.jpg)![](http://p.blog.csdn.net/images/p_blog_csdn_net/Android_Tutor/EntryImages/20091215/sms2.jpg)

Ok~今天到此结束,谢谢大家关注!