博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]android 发送短信和打电话的方法
阅读量:7113 次
发布时间:2019-06-28

本文共 2525 字,大约阅读时间需要 8 分钟。

本文转自:

 

发送短信的方法

 

有两种方法可以实现发送短信,其一是使用intent-startActivityURI数据格式为"smsto:num",调用的actionIntent.ACTION_SENDTO

Uri uri = Uri.parse("smsto:5554");

Intent it = new Intent(Intent.ACTION_SENDTO, uri);

it.putExtra("sms_body", "好。。");

startActivity(it);        

 

 

其二是使用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-permission android: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

EditText edit=(EditText)findViewById(R.id.DialEdit);

String num=edit.getText().toString();

if((num!=null)&&(!"".equals(num.trim())))

{

Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+num));

                    startActivity(intent);

}

打电话权限的设置:

<uses-permission android: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 15555218135

模拟短信发送sms send <senderPhoneNumber> <textmessage>

输入sms send <模拟发送短信的电话> <内容>。如:

sms send 15555218135 hello

其中,15555218135为模拟器手机号码。

 

转载地址:http://kblhl.baihongyu.com/

你可能感兴趣的文章
Gossip算法
查看>>
使用C#或javascript将Table里的数据导出到Excel
查看>>
单调栈小结
查看>>
将Tp-link无线路由器桥接到Dlink无线路由器上
查看>>
Div和Span标签显示与隐藏
查看>>
highcharts 结合phantomjs纯后台生成图片
查看>>
Eclipse上GIT插件EGIT使用手册之十二_重置功能
查看>>
阻塞自定义队列
查看>>
SVG报错error on line 39 at column 26: Namespace prefix xlink for href on script is not defined
查看>>
error: ‘for’ loop initial declarations are only allowed in C99 mode
查看>>
MySQL和Oracle开发差异
查看>>
NTFS For Mac系统配置有什么要求
查看>>
DevExpress的安装方法与破解教程【转】
查看>>
判断浏览器类型的脚本
查看>>
65个面试常见问题技巧回答(绝对实用)
查看>>
又快又省钱这是什么黑科技?图鸭科技带你领略极致直播体验
查看>>
关于不同的MySQL复制解决方案概述
查看>>
学会这个技能,千元机也能拍出炫酷大片
查看>>
手机市场硝烟弥漫,心系天下三星W2017价格上扬仍一机难求
查看>>
蔚来汽车更新招股书:IPO后李斌将拥有48%投票权
查看>>