我想通过Android中的短信发送一些数据(不是多媒体)。这可以做到吗?我只想从一部电话向另一部电话发送非常少量的数据,如XML或其他内容。Android:如何通过短信发送和接收数据
3
A
回答
3
正确的方法是使用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>
+0
谢谢Viktor,看起来不错。但是,这会导致消息的接收者显示一个弹出窗口,询问它想要处理消息的应用程序,还是在后台默默接收? – 2011-06-16 02:26:47
+0
它默默使用,意味着只有一个应用程序应该侦听一个端口上的消息。 – 2012-12-23 14:45:08
1
据我了解你的问题,我会说你可以发送任何类型的文本数据,如XML,JSON字符串或任何通过短信。例如,您可以通过特定的哈希来识别您的数据消息,这些哈希可能是内容的一部分。接收器应该正在监听SMS_RECEIVED意图。
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
这种方式,你可以抓住的数据,之后从消息存储库中删除的消息,以便它不会在谈话中显示出来。
相关问题
- 1. 通过PSTN发送和接收短信
- 2. 通过Arduino发送/接收短信
- 3. 发送和接收短信
- 4. 在android中发送独特的短信和接收looong短信
- 5. 如何通过asp.net建立服务发送/接收短信
- 6. 如何通过广播接收机类别发送短信息
- 7. 发送和接收短信和开发短信面板
- 8. Twilio通过Google Apps脚本发送和接收短信
- 9. 通过TCP接收和发送数据
- 10. Android ListView与收到和发送短信
- 11. 使用Twilio发送和接收短信
- 12. 发送和接收Google短信SMS VB.NET
- 13. android如何发送短信和彩信?
- 14. 如何通过python-gsmmodem接收短信
- 15. 通过SMPP接收短信
- 16. 通过PHP发送短信/短信
- 17. 如何发送短信并在iPhone sdk中接收短信?
- 18. Android:通过闹钟发送短信
- 19. Android通过programetically发送短信
- 20. 通过Android上的PhoneGap发送短信
- 21. 通过Android模拟器发送短信
- 22. Android如何发送Flash(通知)短信?
- 23. 如何通过GPRS发送短信?
- 24. 通过PHP发送短信
- 25. 发送短信通过.net
- 26. 通过IP发送短信
- 27. 通过NFC发送短信
- 28. 通过SMPP发送短信
- 29. 短信网关发送和接收尼日利亚短信
- 30. wp7短信发送接收和短信拦截器
XML ISN对短信的有限字符数非常有效地使用,但这并不会真正影响您的问题。 – BudgieInWA 2010-10-23 22:27:31