前面的范例,示范了如何通过程序拨打电话,在GSM移动通信系统的服务中,除了打电话外,另一个常用的功能,就是发短信.也因为如此,许多电信业者推出专属短信族的专用费率,由此可知短信功能对手机的重要性.
发送短信的关键程序是通过SmsManager对象的sendTextMessage()方法来完成,其中sendTextMessage()方法需传入五个值,依次是收件人地址(String),发送地址(String),发送服务(PendingIntent)与送达服务(PendingIntent),其中收件人与正文是不可为null的两个参数.
本例子通过两个模拟器,5554,5556互相通信,下面我将分5个步骤,讲一下发送短信程序是如何实现的.
Step 1:建立一个Android工程,我们命名为SMSDemo.
Step 2:设计一下程序的UI,也就是主界面main.xml,这里用AbsoluteLayout,有点丑见笑了!代码如下:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="收件 人:"
android:textSize="16sp"
android:layout_x="0px"
android:layout_y="12px"
>
</TextView>
<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"
>
</EditText>
<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"
>
</EditText>
<Button
android:id="@+id/myButton1"
android:layout_width="162px"
android:layout_height="wrap_content"
android:text="发送短信"
android:layout_x="80px"
android:layout_y="302px"
>
</Button>
</AbsoluteLayout>
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代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SMSDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
</manifest>
Step 5:run it!效果图如下,5554给5556发送了一条短信:
Ok~今天到此结束,谢谢大家关注!
分享到:
相关推荐
Android基础教程之----SMS简单发送短信程序(两个模拟器之间的通信)!.doc Android基础教程之----动态更改屏幕方向的简单例子(LANDSCAPE与PORTRAIT)! .doc Android基础教程之----五大布局对象.doc
react-native-sms, 支持iOS和Android的短信发送方 react-native-sms用户定义的SendSMS使用这个 rn 组件发送带有回调 (completed/cancelled/error). iOS和Android的短信。目前,只支持用户发送短信。 this react-na
Android--开发-- SMS 短信内容显示在一个 泡泡 形状的窗口中
阿里云短信集成aliyun-sms-spring-boot-starter
android-sms-gateway, 在Android上,运行简单的短信网关 Android短信网关应用这是一个简单的小应用程序,可以将Google云消息( GCM ) 事件转换为你手机上的outgoing消息。正在启动按照 ...
安卓虚拟平台实现短信的发送功能。具体实现:输入对方电话号码,然后编辑短信内容,选择发送,若发送成功,该代码实现具有提示发送成功功能,等待一会,对方就可以收到并获取信息,发短信是要money的哦!
----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...
百度sms简单发送短信ruby代码
--收到短信开始--> <Item><!--如果有多条循环此节--> <Id>10254</Id> <!-- 编号,唯一值--> <SenderNo>13405886058</SenderNo><!--回复者号码--> 星信:我时工作顺利用这</MsgContent><!--回复短信内容--> <SendTime>...
短信猫发送短信功能,支持PDU中文长短信,7位160个字符,8位140个字符,读短信功能!
电子-sms模拟器实验.rar,单片机/嵌入式STM32-F3/F4/F7/H7
中国网建提供的SMS短信平台http://sms.webchinese.cn 需要的可以看一下
赠送jar包:aliyun-sms-spring-boot-starter-2.0.2.jar 赠送原API文档:aliyun-sms-spring-boot-starter-2.0.2-javadoc.jar 赠送源代码:aliyun-sms-spring-boot-starter-2.0.2-sources.jar 包含翻译后的API文档...
基于android手机系统的 短信 sms 发送接收
由于现有的手机应用程序的开发是构建在各手机厂家的私有操作系统之上的,所以它限制了第三方应用程序的开发,而Android为我们提供了一个开放和通用的选择。因为没有了人为制造的障碍,所以Android开发人员可以自由地...
Android Silent SMS Ping Does not require rooted device SMS will not be delivered as standard SMS, target user will receive no visual notification about SMS being received Description Payload ...
借助SMS Retriever API,您可以在应用程序中自动实施短信确认,而无需任何额外的许可。 用法:该消息应以<#>内容开头:包括验证码的任何文本(例如123456),以及来自密钥库(调试)的sms put >>应用密钥哈希...
本程序的主要功能是短信语音播报,主要运行于android平台,能自动播报预设联系人发来的短信,能给我们的生活带来很大的方便。
Android 短信SMS发送代码流程
Android实现登录系统--QQ登录,SMS,LeanCloud