我想在Android中通过短信发送一些数据(不是多媒体)。这可以做到吗?我只想把极少量的数据从一部手机发送到另一部手机,比如XML之类的。
发布于 2011-06-04 07:38:25
正确的方法是在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> 发布于 2010-10-24 14:53:20
据我所知,您可以通过SMS发送任何类型的文本数据,如XML、JSON字符串等。例如,您可以通过特定的散列来标识接收方上的数据消息,该散列可能是内容的一部分。接收方应该监听SMS_RECEIVED意图。
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>这样,您就可以获取数据,然后从消息存储库中删除消息,这样它就不会出现在会话中。
https://stackoverflow.com/questions/4006360
复制相似问题