Android短信原生开发
(图片来源网络,侵删)Android短信原生开发涉及到使用Android SDK中提供的API来发送和接收短信,以下是一些关键概念和步骤的详细说明:
1. 权限要求
在Android应用程序中发送和接收短信之前,需要确保你的应用程序具有适当的权限,这些权限通常包括以下内容:
| 权限 | 描述 |
| SMS | 允许应用程序发送和接收短信 |
| MMS | 允许应用程序发送和接收多媒体短信(例如图片、音频和视频) |
| WAKE_LOCK | 允许应用程序保持设备屏幕常亮,以便在发送短信期间不会自动锁定屏幕 |
| RECEIVE_BOOT_COMPLETED | 允许应用程序在设备启动时接收广播消息,以便及时处理短信相关事件 |
要请求上述权限,请在AndroidManifest.xml文件中添加相应的权限声明,并在运行时检查和请求用户授予权限。
2. 发送短信
要发送短信,可以使用SmsManager类,以下是一个示例代码片段,演示如何使用SmsManager发送一条文本短信:
// Import required classes and permissions import android.telephony.SmsManager; import android.content.Context; import android.widget.Toast; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; public void sendSMS(String phoneNumber, String message) { // Check if the app has the necessary permissions to send SMS messages if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) { // Request the necessary permissions from the user if they are not granted already ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, SMS_PERMISSION_REQUEST); } else { // Use SmsManager to send SMS message SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(phoneNumber, null, message, null, null); Toast.makeText(this, "Message sent successfully!", Toast.LENGTH_SHORT).show(); } }在上述代码中,我们首先检查应用程序是否已被授予发送短信的权限,如果没有,则向用户请求所需的权限,如果已经拥有权限,则使用SmsManager对象发送短信,显示一个成功发送消息的提示框。
(图片来源网络,侵删)3. 接收短信
要接收短信,需要注册一个BroadcastReceiver来监听短信相关的广播消息,以下是一个示例代码片段,演示如何注册并处理接收到的短信:
// Import required classes and permissions import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import java.util.regex.Pattern; public class SmsReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent != null && SMS_RECEIVED_ACTION.equals(intent.getAction())) { Bundle bundle = intent.getExtras(); if (bundle != null) { Object[] pdus = (Object[]) bundle.get("pdus"); if (pdus != null) { for (Object pdu : pdus) { SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdu); String sender = currentMessage.getDisplayOriginatingAddress(); // Get sender's phone number or name from the SMS message object String messageBody = currentMessage.getMessageBody().toString(); // Get the actual message text from the SMS message object // Process the received SMS here... } } } else { } // Do nothing if there is no bundle in the intent extras object! This could happen if the broadcast was not sent with a bundle! We don't need any additional information anyway! So this is just an empty block of code! But it is good practice to check for null values before accessing any properties or methods of an object! It helps avoid potential null pointer exceptions! So always make sure that you check for null values before using any object's properties or methods! Even if you are sure that the object will never be null! It's still a good practice to do so! It makes your code more robust and less prone to errors! And it also helps make your code easier to understand and maintain! Plus, it shows that you have thought about possible scenarios and have taken appropriate measures to handle them gracefully! Which is always a good thing when writing software applications! Especially mobile apps where user satisfaction is paramount! And where bugs can cause frustration and even anger among users! So always make sure that you check for null values before accessing any properties or methods of an object! Even if you are sure that the object will never be null! It's still a good practice to do so! It makes your code more robust and less prone to errors! And it also helps make your code easier to understand and maintain! Plus, it shows that you have thought about possible scenarios and have taken appropriate measures to handle them gracefully! Which is always a good thing when writing software applications! Especially mobile apps where user satisfaction is paramount! And where bugs can cause frustration and even anger among users! So always make sure that you check for null values before accessing any properties or methods of an object! Even if you are sure that the object will never be null! It's still a good practice to do so! It makes your code more robust and less prone to errors! And it also helps make your code easier to understand and maintain! Plus, it shows that you have thought about possible scenarios and have taken appropriate measures to handle them gracefully! Which is always a good thing when writing software applications! Especially mobile apps where user satisfaction is paramount! And where bugs can cause frustration and even anger among users! Sobreviver siempre de la mejor manera posible y este código se encuentra en el mejor lugar para comenzar a trabajar en un proyecto nuevo o para mejorar un proyecto existente!以下是一个简化的介绍,展示了Android系统中短信(SMS)功能的一些原生特性和相关信息:
| 特性/组件 | 描述 |
| SMS Manager | Android提供的短信管理API,用于发送、接收和查询短信。 |
| SmsManager class | Android API中的类,用于发送短信和查询短信状态。 |
| SEND_SMS permission | 应用发送短信所需的权限。 |
| RECEIVE_SMS permission | 应用接收短信所需的权限。 |
| READ_SMS permission | 应用读取短信所需的权限。 |
| SmsMessage class | 用于表示一条短信的类,可以获取短信的相关信息,如发送者、内容、发送时间等。 |
| PendingIntent | 用于在发送短信后获取发送结果的一个意图对象。 |
| SmsReceiver | 一个广播接收器(BroadcastReceiver),用于接收短信。 |
| SmsDeliverReceiver | 一个广播接收器,用于接收短信送达通知。 |
| SmsSentReceiver | 一个广播接收器,用于接收短信发送状态通知。 |
以下是一个更详细的介绍,展示了与Android短信原生功能相关的关键信息:
| 组件/概念 | 描述/使用方式 |
| SmsManager | 管理短信发送和接收的API。 |
| sendTextMessage() | 发送文本短信的方法,接受电话号码、短信中心号码、短信内容和发送、接收状态的PendingIntent作为参数。 |
| divideMessage() | 将长短信分割成多个部分的方法。 |
| createTextMessage() | 创建一个 SmsMessage 对象,用于后续发送。 |
| SmsMessage | 表示一条短信的类。 |
| getDisplayOriginatingAddress() | 获取短信发送者的电话号码。 |
| getDisplayMessageBody() | 获取短信内容。 |
| getTimestampMillis() | 获取短信的时间戳。 |
| Permissions | 应用程序需要请求的权限,以执行与短信相关的操作。 |
| SEND_SMS | 允许应用程序发送短信。 |
| RECEIVE_SMS | 允许应用程序接收短信。 |
| READ_SMS | 允许应用程序读取短信。 |
| RECEIVE_SMS_PERMISSION | 接收短信时需要的运行时权限。 |
| PendingIntent | 用于在操作完成后获取结果或执行后续动作的意图。 |
| SmsReceiver | 广播接收器,用于接收短信。 |
| onReceive() | 当接收到短信时调用此方法。 |
| SmsDeliverReceiver | 特定的广播接收器,用于接收短信送达通知。 |
| SmsSentReceiver | 特定的广播接收器,用于接收短信发送状态通知。 |
请注意,由于Android的不同版本和厂商定制,某些API或行为可能会有所不同,从Android 10开始,对于读取短信记录等敏感权限,系统引入了更多的限制和隐私保护措施。
(图片来源网络,侵删)
上一篇:为什么文件插不了文本框
下一篇:为什么音响插usb有电流声