Android 调用系统接口发短信
来源:互联网 发布:c语言基础课程 编辑:程序博客网 时间:2024/05/24 04:34
(1) 通过系统接口发短信,首先需要获取短信管理器:
android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();
(2)然后因为手机短信有长度限制,所以需要拆分短信内容
List<String> divideContents = smsManager.divideMessage(message);
这里大家可能会有个疑问,如果不把短信拆分会怎么样,是不是就不能发送短信了。
本人亲测后,若短信内容比长度限制少,这短信可以发送出去。若短信内容超过了长度设置,则无法发送。所以通常都会把这一步加上。
(3) 然后就可以发短信了
for (String text : divideContents) {smsManager.sendTextMessage(phoneNumber, null, text, sendIntent,backIntent);}
public void sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sendIntent, PendingIntent deliveryIntent)这是SmsManager.sendTextMessage的方法原型。
其中参数的意思分别为:
destinationAddress:发送短信的目标地址(手机号码)
</pre><pre name="code" class="java">scAddress:短信服务中心,如果为null,就是用当前默认的短信服务中心
</pre><pre name="code" class="java">text:短信内容
</pre><p></p><pre>
sendIntent:如果不为null,当短信发送成功或者失败时,这个PendingIntent会被广播出去成功的结果代码是Activity.RESULT_OK,或者下面这些错误之一 <span style="font-family: Arial, Helvetica, sans-serif;">RESULT_ERROR_GENERIC_FAILURE,RESULT_ERROR_RADIO_OFF,RESULT_ERROR_NULL_PDU.对于RESULT_ERROR_GENERIC_FAILURE, 这个sentIntent可能包括额外的"errorCode",包含一些具体有用的信息帮助检查 。基于SMS控制的全部程序检查 sentIntent. 如果 sendIntent 为空,the caller will be checked against all unknown applications, which cause smaller number of SMS to be sent in checking period.
deliveryIntent:如果不为null,当这个短信发送到接收者那里,这个PendtingIntent会被广播,状态报告生成的pdu(指对等层次之间传递的数据单位)会拓展到数据("pdu")(4)处理返回的发送报告
String SENT_SMS_ACTION = "SENT_SMS_ACTION";Intent sentIntent = new Intent(SENT_SMS_ACTION);PendingIntent sendIntent= PendingIntent.getBroadcast(context, 0, sentIntent, 0);// register the Broadcast Receiverscontext.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context _context, Intent _intent) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT) .show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: break; case SmsManager.RESULT_ERROR_RADIO_OFF: break; case SmsManager.RESULT_ERROR_NULL_PDU: break; default: } }}, new IntentFilter(SENT_SMS_ACTION));若发送成功,则会弹出一个对话框显示“短信发送成功”。
(5)处理返回的接受报告
//处理返回的接收状态 String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";// create the deilverIntent parameterIntent deliverIntent = new Intent(DELIVERED_SMS_ACTION);PendingIntent backIntent= PendingIntent.getBroadcast(context, 0, deliverIntent, 0);context.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context _context, Intent _intent) { Toast.makeText(context, "收信人已经成功接收", Toast.LENGTH_SHORT) .show(); }}, new IntentFilter(DELIVERED_SMS_ACTION));
若对方接受成功,则会弹出一个对话框显示“收信人已经成功接受”。
演示结果:
在第一张图的第一个编辑框可以输入,你要发短信的号码。而在第二个编辑框可以输入,发送短信的内容。按下“发送短信!”按钮则会发送短信。
从第二张图可以看到,但短信发送成功,则会有对话框提醒。
同样当对方成功接受短信,也会有对话框提醒。
0 1
- Android 调用系统接口发短信
- 调用Android发短信接口Intent.ACTION_SENDTO
- 调用接口发短信
- 调用系统发短信
- java调用发短信接口
- iOS开发之调用系统打电话发短信接口以及程序内发短信
- iOS开发之调用系统打电话发短信接口以及程序内发短信
- 调用系统打电话发短信接口以及程序内发短信
- Android应用开发笔记(1) 调用打电话和发短信、收短信接口、发Email
- 调用系统发短信服务
- 系统调用打电话,发短信
- 【Android基础】调用系统发Email和短信
- android之调用系统打电话与发短信界面
- android之调用系统打电话与发短信界面
- android之调用系统打电话与发短信界面
- android调用系统打电话发短信qq会话
- Android调用系统的打电话和发短信功能
- Android调用系统打电话和发短信功能
- ASP程序中RecordCount=-1解决方案
- 选择日期Excel日历控件将日期插入到指定单元格实例
- 调用Google翻译 语音接口
- DT系列五)Linux kernel 是怎么将 devicetree中的内容生成plateform_device
- JSP调用SQL server 2000存储过程的方法
- Android 调用系统接口发短信
- MVC操作 DropDownList
- 记录show parameter格式化
- std::string类详解
- OC高级语法之KVC和KVO的学习
- WPF rotate animation
- 【算法导论】动态规划之“矩阵链乘法”问题
- Java中守护线程的总结
- v4l2_field理解