zoukankan      html  css  js  c++  java
  • android 的短信发送

    1: android 的短信发送可以在模拟器中进行模拟出来。
    如现在启动一模拟器id 号为5554,
    运行cmd
    telnet localhost 5554
    输入help 可以看到很多用于模拟器中的功能命令
    gsm call 134343434 // 便是呼叫当前模拟器命令
    sms send 15555218135 Hello,this is a Message // 是向当前的模拟器发送短信息

    2: 相关类:
    android.telephony.gsm.SmsManager
    android.telephony.gsm.SmsMessage
    android.app.PendingIntent
    android.widget.Toast

    3:实现代码(节选)
    String msg ="hello";
    string number = "1234565678";
    SmsManager sms = SmsManager.getDefault();

    PendingIntent pi = PendingIntent.getBroadcast(Sms.this,0,new Intent(),0);
    sms.sendTextMessage(number,null,msg,pi,null);
    Toast.makeText(Sms.this,"send success",Toast.LENGHT_LONG).show();

    4:代码解释
    上述发送短信的代码很简单,但是其中的几个类函数并不好理解:

    Toast.makeText 就是展示一个提示信息,这个比较容易理解;

    PendingIntent 就是一个Intent 的描述,我们可以把这个描述交给别的程序,别的程序
    根据这个描述在后面的别的时间做你安排做的事情,By giving a PendingIntent to another application,
    you are granting it the right to perform the operation you have specified as if the other
    application was yourself,就相当于你的代表了。本例中别的程序就是发送短信的程序,短信发送成功后要把intent 广播出去 。


    函数sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
    前三个参数按照文档比较容易理解,
    PendingIntent  sentIntent 当短信发出时,成功的话sendIntent会把其内部的描述的intent广播出去,否则产生错误代码并通过android.app.PendingIntent.OnFinished
    进行回调,这个参数最好不为空,否则会存在资源浪费的潜在问题;
    deliveryIntent  是当消息已经传递给收信人后所进行的PendingIntent 广播。

    查看PendingIntent 类可以看到许多的Send函数,就是PendingIntent在进行被赋予的相关的操作。
  • 相关阅读:
    jQuery EasyUI实现全部关闭tabs
    设计与实现模块管理系统基本功能定义自己(28--所述多个模块之间的关联[4])
    C++11的一些功能
    表和视图之间的区别
    三个思路来实现自己定义404页面
    WebGL 在 OpenGL ES 指令 iOS 在 C 分歧版指令分析
    hdoj 2183 奇数阶魔方(II) 【模拟】+【法】
    新浪、万网前系统架构师高俊峰:统一监控报警平台架构设计思路
    this compilation unit is not on the build path of a java project
    Ecshop wap
  • 原文地址:https://www.cnblogs.com/tt_mc/p/1678746.html
Copyright © 2011-2022 走看看