gpt4 book ai didi

Android:如何通过短信发送和接收数据

转载 作者:太空宇宙 更新时间:2023-11-03 13:38:14 26 4
gpt4 key购买 nike

我想在 Android 中通过短信发送一些数据(不是多媒体)。这可以做到吗?我只想将非常少量的数据从一部手机发送到另一部手机,例如 XML 或其他格式。

最佳答案

执行此操作的正确方法是使用 SmsManager 类中的 sendDataMessage。这是一小段代码 (SMSSender):

SmsManager smsMgr = SmsManager.getDefault();
smsMgr.sendDataMessage(phoneNumber, null,
(short) myApplicationPort, messageString.getBytes(), sentIntent, deliveryIntent);

这是另一个小代码(SMSReceiver):

    Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdusObj.length];

// getting SMS information from PDU
for (int i = 0; i < pdusObj.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
}

for (SmsMessage currentMessage : messages) {
if (!currentMessage.isStatusReportMessage()) {

String messageBody = currentMessage.getDisplayMessageBody();

byte[] messageByteArray = currentMessage.getPdu();

// skipping PDU header, keeping only message body
int x = 1 + messageByteArray[0] + 19 + 7;

// I'm not sure about this last line, as I'm not converting the bytes back to string, so test it out
String realMessage = new String(messageByteArray, x, messageByteArray.length-x);

以下是您应该添加到 AndroidManifest.xml 中的内容:

 <receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:host="localhost" />
<data android:port="12345" /><!-- this number should be the same as the `myApplicationPort` from above!!! -->
</intent-filter>
</receiver>

关于Android:如何通过短信发送和接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4006360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com