android基础---->发送和接收短信

时间:2022-06-09 09:05:44

  收发短信应该是每个手机最基本的功能之一了,即使是许多年前的老手机也都会具备这项功能,而Android 作为出色的智能手机操作系统,自然也少不了在这方面的支持。今天我们开始自己创建一个简单的发送和接收短信的应用。

目录导航

  1. 接收短信
  2. 发送短信
  3. 友情链接

接收短信

项目的结构如下:一个简单的接收和发送短信的功能

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPgAAAD9CAIAAAAefi3vAAATlklEQVR4nO2dy28V1x3H71/RXTdZdNVNF60YqUWIgGoVwYpNwqaKqrsIKxTZCqHFiVSFvBihRoK8qqBUDY2Msanh8iiBYKMAwuEdA9eApWLjSCHYGF8TN6DpYu6dOc95z50z8/t+NIt75845M5iPx2fmnu/8ag4ABKgVfQAAdAOIDkiQVvQvd78kLHcvHMzkyADIkOxFh+vAQHIRXblkcrhdo2lbtZplN7vRCnSBmuM4hw4dGlQxMjIS2r6qojfqtQTKJmsFukDNcZyZmRml6DMzM6Hto0is2KZpW64SjXqt1pGj/Zo1pbOKXemvq9UbyjVu71Jn3up6gzdS3AvTn2IfuiNRtQKm0B66HD9+XLD8+PHjUdqnE91ihGReq3xry8M6rFsjNnSlE3sL2IukbKQjgegm0xZ9YWHhwIEDnuUHDhxYWFiI0j6K6NPXT01fP8WtalvCnX/ZN75drjHe4FceHAQOF/wP3d7a2zFvlHsReo14JBi6GIx/MXrmzBlP9LGxsYjtA0Tfsu+ssPifeUMXx+E98t/wZ0zVyZVrzq4Rmlp2k98hY6RmL5yykY8EopuLL/ry8vLQ0JB7Ol9eXo7YPm/R1dZ4jnkjBGYNe+tDFJrbieIXgN8DJ3qUI4HoxsLdXrx06dLg4OCVK1eit89PdPVQW14nrZFOwJbdFE7L7gWBcowuXXsqxugRjg5jdMMQ76OfOHEiVnvdzcTp66dSiy7YKekliyoMZizblofatZplN7hztLwXh7tFE/VI5FbAGPL6wihE9GJpOwoZCZHjpC7DRBdvEWIoTQo6sxdxm5s0dEQHpIHog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ztDXSJgkUfGRmZnZ1N0DBF5eg4Z/S6fPWYKJ6s7Q10iUp9M5rpffRsRxcYqxRMWUXPH4heKSC6DoheKSA6IAFEBySA6IAEEB2QAKIDEkB0QAKIDkhQVtERjgaxqI7ombpemufbg4ggHK0EolcNI6bpmheOhuhVwwjRzQtHQ/SqYYroXQlHKyo48xPI2VCnECHVFl3hOsR0c1MxS/Tcw9FyBWdtPegA0YXabkIlaGAiZonevXC0VNyTPxkHii6W0PIidLWAuomgWAwSvSvhaGUFZznLHCa61mf3Vwa6G4cRoncxHC2VZtbWg/ZEF4vMCo9ladTbW9qqsTwwBGrhaEfK4CuzzP5dF+9jTbVo1fUpNDePSn0zmmk4GlSKsooOQCwgOiABRAckgOiABBAdkACiAxJAdECC/wMa3GNLzRv2owAAAABJRU5ErkJggg==" alt="" />

一、定义一个接收短信的广播:当手机接收到一条短信的时候,系统会发出一条值为android.provider.Telephony.SMS_RECEIVED 的广播,这条广播里携带着与短信相关的所有数据。

// 接收短信的广播
class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus"); // 提取短信消息
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
String address = messages[0].getOriginatingAddress(); // 获取发送方号码
String fullMessage = "";
for (SmsMessage message : messages) {
fullMessage += message.getMessageBody(); // 获取短信内容
}
textView.setText("address; " + address + ", message: " + fullMessage);
abortBroadcast();
}
}

二、 注册广播,这里我们动态在代码中注册:

// 注册接收广播
IntentFilter intentFilter1 = new IntentFilter();
intentFilter1.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
intentFilter1.addAction("android.provider.Telephony.SMS_RECEIVED");
messageReceiver = new MessageReceiver();
registerReceiver(messageReceiver, intentFilter1);

三、 声明权限:

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />

发送短信

一、 发送短信:用getDefault方法得到SmsManager对象,调用sendTextMessage方法发送短信

// 发送短信
public void send(View view) {
SmsManager smsManager = SmsManager.getDefault();
String destinationAddress = "15527100207";
String text = "我爱你!"; Intent intent1 = new Intent(SENT_SMS_ACTION);
PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0, intent1, 0);
Intent intent2 = new Intent(DELIVERY_SMS_ACTION);
PendingIntent deliveryIntent = PendingIntent.getBroadcast(this, 0, intent2, 0);
smsManager.sendTextMessage(destinationAddress, null, text, sentIntent, deliveryIntent); // aidl服务,进程间的通信
}

我们具体了解一下sendTextMessage方法一个参数的含义:

public void sendTextMessage(String destinationAddress, String scAddress, String text,PendingIntent sentIntent, PendingIntent deliveryIntent)
destinationAddress: // 目的地,也就是对方的手机号
scAddress: // 服务中心地址,为空的话就是默认的SMSC
text: // 发送消息的具体内容
sentIntent: // 当消息成功或者失败发送时,就发起这个广播
deliveryIntent: // 当消息到达目的地时,就发起这个广播

三、 sentIntent的广播:

// 发送短信的状态广播
class MessageBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 一个10位数的号码发送,也能成功,但是对方没有接收短信。
if (getResultCode() == RESULT_OK) {
Toast.makeText(MainActivity.this, "发送成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "发送失败!", Toast.LENGTH_SHORT).show();
}
}
}

四、 deliveryIntent的广播:

// 当消息被传递给收件人时
class DeliveryBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(MainActivity.this, "对方接收短信成功!", Toast.LENGTH_SHORT).show();
}
}

友情链接