【问题标题】:How to send non-printing characters via SMS如何通过 SMS 发送非打印字符
【发布时间】:2011-12-30 03:53:49
【问题描述】:

有人知道如何在 Android 中通过 SMS 发送非打印字符吗?

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

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

或者有没有其他方法可以在短信中插入某种标签,以便接收者可以相应地执行一些操作?

【问题讨论】:

  • “紧急”不会为你赢得任何好感,更有可能适得其反!

标签: android sms character


【解决方案1】:

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

【讨论】:

  • 您好,需要GPRS/3G等吗?或者任何手机都可以发送二进制短信?
  • 任何手机都可以发送二进制短信。
  • 二进制短信标准赋予您处理数据的全部权力。您必须为二进制消息编写自定义序列化器/反序列化器。尝试通过二进制文件查找 sendind 的源代码。实现它是一个漫长的故事。
【解决方案2】:

由于该问题上有一个 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);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 1970-01-01
    • 2022-10-04
    • 1970-01-01
    相关资源
    最近更新 更多