Android多媒体应用——短信收发

短信收发在应用中我们也经常会用到,特别是短信的发送。

那么,在android中如何实现呢?

短信发送

短信的发送是很简单的。android提供了一个短信管理类来处理,具体代码如下:
SmsManager smsManager=SmsManager.getDefault();
smsManager.sendTextMessage(sendPhone.getText().toString(), null, sendText.getText().toString(), null, null);
这两句代码就实现了短信的发送,当然,你要在AndroidManifest.xml中添加上权限,允许发送短信:
 <uses-permission android:name="android.permission.SEND_SMS"/>
短信的发送的核心代码就是这些。
这里你可能对sendTextMessage的参数有疑惑,除了号码和内容外还有什么呢?
destinationAddress:  the address to send the message to 
scAddress:  is the service center address or null to use the current default SMSC 
text : the body of the message to send 
sentIntent:  if not NULL this PendingIntent is broadcast when the message is successfully sent, or failed. The result code will be Activity.RESULT_OK for success, or one of these errors:
RESULT_ERROR_GENERIC_FAILURE
RESULT_ERROR_RADIO_OFF
RESULT_ERROR_NULL_PDU
For RESULT_ERROR_GENERIC_FAILURE the sentIntent may include the extra "errorCode" containing a radio technology specific value, generally only useful for troubleshooting.
The per-application based SMS control checks sentIntent. If sentIntent is NULL the caller will be checked against all unknown applications, which cause smaller number of SMS to be sent in checking period. 
deliveryIntent:  if not NULL this PendingIntent is broadcast when the message is delivered to the recipient. The raw pdu of the status report is in the extended data ("pdu"). 
以上是API中对它们的解释,简单点翻译,如下:
destinationAddress:目标的电话号码;
<pre name="code" class="html">scAddress:短信服务中心,如果写null,就用当前默认的服务中心;
<pre name="code" class="html">text :短信内容;
<pre name="code" class="html">sentIntent:这是一个pendingintent,用于监听短信的发送状态;
deliveryIntent:<span style="font-family: Arial, Helvetica, sans-serif;">这是一个pendingintent,用于监听短信的被接收状态;</span>
 
 
 
 
 
 

如果,你想监听短信是否发送成功,可以补全sendTextMessage的第三个参数,将它改写为:
PendingIntent pIntent=PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(sendFlag), 0);
SmsManager smsManager=SmsManager.getDefault();
smsManager.sendTextMessage(sendPhone.getText().toString(), null, sendText.getText().toString(), pIntent, null);
其中,sendFlag为:
String sendFlag="SENT_SMS_ACTION";  
这样就可以在广播BroadcastReceiver的onReceive方法中监听是否短信发送成功。

还有一个监听短信是否成功被接收,你也可以在源码的基础上添加,广播过滤是   "DELIVERED_SMS_ACTION"。

短信接收

短信的接收,用到的就是广播。监听系统广播"android.provider.Telephony.SMS_RECEIVED",当收到该广播时,解析该广播带的数据。这就是短信信息。
以下是短信接收的核心解析代码:
                               public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				if(receiverFlag.equals(intent.getAction()))
				{
					if(getResultCode()==RESULT_OK)
					{
						Bundle bundle=intent.getExtras();
						Object[] objects=(Object[])bundle.get("pdus");
						SmsMessage[] smsMessages=new SmsMessage[objects.length];
						for(int i=0;i<objects.length;i++)
						{
							smsMessages[i]=SmsMessage.createFromPdu((byte[])objects[i]);
						}
						
						String phoneString="来信号码:";
						String contentString="来信信息:";
						for (SmsMessage smsMessage : smsMessages) {
							phoneString=smsMessage.getDisplayOriginatingAddress();
							contentString+=smsMessage.getDisplayMessageBody();
						}
						recevicePhone.setText(phoneString);
						receviceText.setText(contentString);
					}
步骤如下:取广播附带信息-》取其中key为“pdus”-》转为object[]类型,存储数据-》新建短信数组,并将object[i]值赋值给短信数组-》在短信数组中取出号码和串联在一起的短信数据。

源码

以上短信的发送接收源码路径:
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值