Android短信发送功能实现技巧分享

编程人员能够对Android手机操做系统进行一些更改来知足用户的需求。不过要想对其进行修改,首先须要了解其源码的编写方式。在这里咱们先来看看Android短信功能的具体实现,来体验一下相关编写方式。 html

1: Android短信发送能够在模拟器中进行模拟出来。  编程

如如今启动一模拟器id 号为5554,运行cmd 
telnet localhost 5554 网络

输入help 能够看到不少用于模拟器中的功能命令 app

 
  1. gsm call 134343434   // 即是呼叫当前模拟器命令   
  2. sms send 15555218135 Hello,this is a Message   // 是向当前的模拟器发送短信息  

2: 相关类:  函数

 
  1. Android.telephony.gsm.SmsManager   Android.telephony.gsm.SmsMessage   
  2. Android.app.PendingIntent   Android.widget.Toast  


3:Android短信发送实现代码(节选)  this

 
  1. String msg ="hello";   string number = "1234565678";   
  2. SmsManager sms = SmsManager.getDefault();   PendingIntent pi = PendingIntent. getBroadcast(Sms.this,0,new Intent(),0);   
  3. sms.sendTextMessage(number,null,msg,pi,null);   Toast.makeText(Sms.this,"send success", Toast.LENGHT_LONG).show();  


4:Android短信发送代码解释  操作系统

上述发送短信的代码很简单,可是其中的几个类函数并很差理解: Android重力感应实现方式简介Android Theme详细内容概述Android应用技巧总结Android显示网络图片相关实现方法浅谈Android开机自启动具体操做方法简介 code

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

PendingIntent 就是一个Intent 的描述,咱们能够把这个描述交给别的程序,别的程序 htm

根据这个描述在后面的别的时间作你安排作的事情,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在进行被赋予的相关的操做。

Android短信发送的相关实现方法就为你们介绍到这里。

原文:http://www.baisoujs.com/detail_137070713892117.html

相关文章
相关标签/搜索