短信收发在应用中我们也经常会用到,特别是短信的发送。
那么,在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]值赋值给短信数组-》在短信数组中取出号码和串联在一起的短信数据。
源码
以上短信的发送接收源码路径: