如何通过短信发送非打印字符

时间:2011-12-30 03:53:50

标签: android sms character

任何人都知道如何在Android中通过短信发送非打印字符?

我尝试了以下代码,但它不起作用......收件人将收不到正确的字符串。

String msg = "Testing special char" +(char) 3;
sendSMS(num,msg);//defined method

或者是否有其他方法可以在SMS中插入某种标签,以便收件人可以相应地执行某些操作?

2 个答案:

答案 0 :(得分:2)

默认情况下,您以ascii格式发送短信。尝试发送二进制短信。

答案 1 :(得分:0)

由于问题上有Android标记,这是我在研究主题时发现的(来自codetheory.in的代码)。

发送:

// Get the default instance of SmsManager
SmsManager smsManager = SmsManager.getDefault();

String phoneNumber = "9999999999";
byte[] smsBody = "Let me know if you get this SMS".getBytes();
short port = 6734;

// Send a text based SMS
smsManager.sendDataMessage(phoneNumber, null, port, smsBody, null, null);

接收:

public class SmsReceiver extends BroadcastReceiver {
    private String TAG = SmsReceiver.class.getSimpleName();

    public SmsReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // Get the data (SMS data) bound to intent
        Bundle bundle = intent.getExtras();

        SmsMessage[] msgs = null;

        String str = "";

        if (bundle != null){
            // Retrieve the Binary SMS data
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];

            // For every SMS message received (although multipart is not supported with binary)
            for (int i=0; i<msgs.length; i++) {
                byte[] data = null;

                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

                str += "Binary SMS from " + msgs[i].getOriginatingAddress() + " :";

                str += "\nBINARY MESSAGE: ";

                // Return the User Data section minus the
                // User Data Header (UDH) (if there is any UDH at all)
                data = msgs[i].getUserData();

                // Generally you can do away with this for loop
                // You'll just need the next for loop
                for (int index=0; index < data.length; index++) {
                    str += Byte.toString(data[index]);
                }

                str += "\nTEXT MESSAGE (FROM BINARY): ";

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

                str += "\n";
            }

            // Dump the entire message
            // Toast.makeText(context, str, Toast.LENGTH_LONG).show();
            Log.d(TAG, str);
        }
    }
}