在android中有两种方式发送短信:
第一种:
使用
intent-startActivity
,
URI
数据格式为"
smsto:num
",调用的
action
为
Intent.ACTION_SENDTO
:
Intent tent = new Intent();
tent.setAction(Intent.ACTION_SENDTO);
tent.setData(Uri.parse("smsto:5554"));
tent.putExtra("sms_body", "android 你好!");
startActivity(tent);
第二种:
使用SmsManager
EditText num=(EditText)findViewById(R.id.num);
EditText content=(EditText)findViewById(R.id.content);
String mobile=num.getText().toString();
String smstext=content.getText().toString();
//获取SmsManager
SmsManager sms=SmsManager.getDefault();
//如果内容大于70字,则拆分为多条
List<String> texts=sms.divideMessage(smstext);
//逐条发送短信
for(String text:texts)
{
sms.sendTextMessage(mobile, null, text, null, null);
}
//发送结果提示
Toast.makeText(SendSMS.this, "发送成功", Toast.LENGTH_LONG).show();
二者的不同在于前者只是调用了发送界面,需要按下Send按钮短信才发送出去,而后者则是直接发送出去。
发送SMS权限的设置:
<uses-permissionandroid:name="android.permission.SEND_SMS"/>
关于SmsManager
SDK
中的介绍:
Manages
SMS operations such as sending data, text, and pdu SMS messages. Get
this object by calling the static method SmsManager.getDefault().
方法:
public void sendTextMessage (String destinationAddress,
String scAddress, String text, PendingIntent sentIntent, PendingIntent
deliveryIntent)
destinationAddress:
收件人地址
scAddress:
短信中心号码,null为默认中心号码
sentIntent:
当消息发出时,成功或者失败的信息报告通过PendingIntent来广播。如果该参数为空,则发信程序会被所有位置程序检查一遍,这样会导致发送时间延长。
deliveryIntent:
当消息发送到收件人时,该PendingIntent会被广播。pdu数据在状态报告的extended data ("pdu")中。
如果收件人或者信息为空则抛出 IllegalArgumentException 。
public ArrayList<String> divideMessage (String text)
将大于70字的短信分割为多条。
参数:text the original message. Must not be null.
返回:an ArrayList of strings that, in order, comprise the original message
sendDataMessage 参数与上类似,只是用于发送Data。
sendMultipartTextMessage发送多条短信,发送内容必须是用divideMessage分割好了的。
打电话的方法
打电话的方法类似,所不同的是URI格式为"tel:num",而调用的action为Intent.ACTION_CALL
:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:18982113450"));
startActivity(intent);
打电话权限的设置:
<uses-permissionandroid:name="android.permission.CALL_PHONE"/>
扩展学习:
向模拟器发短信打电话的方法
1.启动android emulator,查看标题栏找出端口。一般是android emulator (5554),其中5554就是端口。
2.打开命令行,输入telnet localhost 5554。程序将会连接到android console,返回
Android Console: type 'help' for a list of commands
OK
模拟电话打入gsm <call|accept|busy|cancel|data|hold|list|voice|status>
输入gsm call <模拟打进的电话号码>。如:
gsm call 18982113450
模拟短信发送sms send <senderPhoneNumber> <textmessage>
输入sms send <模拟发送短信的电话> <内容>。如:
sms send 18982113450 hello
其中,18982113450为模拟器手机号码。
分享到:
相关推荐
code 博文链接:https://limdengrock.iteye.com/blog/1018141
( 菜鸟学习Foxtable开发.pptx )
Android应用开发入门教程(经典版)&android;快速入门 Android应用开发入门教程(经典版)&android;快速入门
Android开发实例:手机助手 源码
嵌入式系统开发之道:菜鸟成长日志与项目经理的私房菜 Part2 免费!
我和菜鸟一起学android4.0.3源码之硬件gps简单移植
Android开发入门之路(初学者必读).zip
# 菜鸟商城 # 是一个仿淘宝客户端的实战课程,功能包括:1.支付(支付宝,微信,百度钱包) 、首页 、热卖 、商品大全 、购物车 、我的 、商品列表 、商品详情 、注册/登录 、收货地址 、我的订单 、我的收藏 ..... ...
基于VS2012的菜鸟WDF驱动开发系列
菜鸟学习Foxtable开发.pptx
菜鸟系列之一:在UBUNTU LINUX 下搭建ANDROID开发平台。 本文档系统的整理了在ubuntu系统下如何搭建android开发平台,对android感兴趣的同学可以借鉴学习下。
电子-嵌入式系统开发之道菜鸟成长日志与项目经理的私房菜.part2.rar,综合电子技术数字,模拟,高频电路
android商城源码,功能齐全,注释明确,是学习,二次开发的最佳选择,带数据
嵌入式系统开发之道菜鸟成长日志与项目经理的私房菜pdf文件,非扫描版哦, 对开发者还是有些帮助的。
自己用半年所有的业余时间从根本没怎么学习过android的菜鸟不断学习开发了一个基于地图的聊天app做工比较粗糙但基本功能都有了。因为种种原因不能把它做成一款商业产品又不想把它埋没了。现在我把这个分享给大家希望...
从菜鸟到高手 Android系列问答汇总
菜鸟学习Foxtable开发1.pptx
为此,“职场大菠菜”携手"指尖疯教育"重磅推出《安卓开发实战免费微课》系列!91集微课,足以让您从咿呀学语的菜鸟成功晋级内功深厚的高手。 微课也要有力量,免费也要有匠心!从PPT课件的高颜值梳妆,到产品制作...
嵌入式系统开发之道 菜鸟成长日志与项目经理的私房菜,部分章节
Android开发环境配置,菜鸟级入门资料