如何发送和接收数据短信

我找到了一些有关如何发送/接收文本SMS消息的教程,但是没有关于如何发送/接收数据SMS消息的教程。我希望我的应用程序的用户能够共享很少的数据。


我可以发送,但是我BroadcastReceiver从未接到过电话。看来这是一个已知问题(http://code.google.com/p/android/issues/detail?id=1576),但是有人知道如何做到这一点吗?


我尝试发送/接收短信,但效果很好,问题是,我需要指定端口,以便只有我的应用才能收听短信。


holdtom
浏览 331回答 1
1回答

冉冉说

我知道我在回复时只有1岁,但我认为它仍然可以帮助某人。接收:Bundle bundle = intent.getExtras();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String recMsgString = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fromAddress = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SmsMessage recMsg = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] data = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (bundle != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //---retrieve the SMS message received---&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Object[] pdus = (Object[]) bundle.get("pdus");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<pdus.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recMsg = SmsMessage.createFromPdu((byte[])pdus[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = recMsg.getUserData();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (data!=null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int index=0; index<data.length; ++index)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;recMsgString += Character.toString((char)data[index]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fromAddress = recMsg.getOriginatingAddress();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }在清单中设置接收器:<receiver android:name=".SMSReceiver">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; <action android:name="android.intent.action.DATA_SMS_RECEIVED" />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <data android:scheme="sms" />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <data android:port="8901" />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </intent-filter>&nbsp;</receiver>&nbsp;正在发送:String messageText = "message!";&nbsp;short SMS_PORT = 8901; //you can use a different port if you'd like. I believe it just has to be an int value.SmsManager smsManager = SmsManager.getDefault();&nbsp;smsManager.sendDataMessage("8675309", null, SMS_PORT, messageText.getBytes(), null, null);&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android