如何发送和接收数据的SMS消息消息、数据、SMS

2023-09-12 01:16:38 作者:眼中星河已尽

我已经找到了如何发送/接收文本短信,但是没有就如何发送/接收数据短信的几个教程。我有一个非常小的数据量,我想我的应用程序的用户能够共享。

I've found a few tutorials on how to send/receive text SMS messages, but none on how to send/receive data SMS messages. I have a very small amount of data I would like the users of my app to be able to share.

我可以送,但我的的BroadcastReceiver 不会永远被调用。看来这是一个已知的问题(的http:// code.google.com / P /安卓/问题/详细信息?ID = 1576 ),但有没有人知道如何做到这一点了吗?

I am able to send, but my BroadcastReceiver doesn't ever get called. It seems this is a known issue (http://code.google.com/p/android/issues/detail?id=1576) but has anyone figured out how to do this yet?

我试着发送/接收文本短信和工作正常,问题是,我需要指定一个端口,因此只有我的应用程序可以侦听短信。

I tried sending/receiving a text SMS and that works fine, the thing is, I need to specify a port so only my app can listen for the SMS.

看来这个问题已经被问这里之前从未回答:的如何接收文本短信到特定端口..

It seems this question has been asked here before and was never answered: how to receive text sms to specific port..

推荐答案

我知道这是1年我的回应的时候了,但我认为它仍然可以帮助别人。 接收:

I know this is 1 year old at time of my response, but I thought it could still help someone. Receiving:

Bundle bundle = intent.getExtras(); 

            String recMsgString = "";            
            String fromAddress = "";
            SmsMessage recMsg = null;
            byte[] data = null;
            if (bundle != null)
            {
                //---retrieve the SMS message received---
               Object[] pdus = (Object[]) bundle.get("pdus");
                for (int i=0; i<pdus.length; i++){
                    recMsg = SmsMessage.createFromPdu((byte[])pdus[i]);

                    try {
                        data = recMsg.getUserData();
                    } catch (Exception e){

                    }
                    if (data!=null){
                        for(int index=0; index<data.length; ++index)
                        {
                               recMsgString += Character.toString((char)data[index]);
                        } 
                    }

                    fromAddress = recMsg.getOriginatingAddress();
                }

设置接收机的清单:

<receiver android:name=".SMSReceiver"> 
        <intent-filter>
        <action android:name="android.intent.action.DATA_SMS_RECEIVED" /> 
            <data android:scheme="sms" /> 
            <data android:port="8901" /> 
        </intent-filter> 
</receiver> 

发送:

String messageText = "message!"; 
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(); 
smsManager.sendDataMessage("8675309", null, SMS_PORT, messageText.getBytes(), null, null);