Xamarin Android接收和发送短信超过160个字符

时间:2017-01-18 11:02:32

标签: xamarin xamarin.android sms

我正在写短信应用程序,它通过restapi号码和服务器从服务器获取发送的地址,并从receipent接收短信。当收到短信超过160个字符时,我有问题。问题是当我通过短信发送超过160个字符时。

接收短信代码:

public override void OnReceive(Context context, Intent intent)
    {
        if (intent.HasExtra("pdus"))
        {
            var smsArray = (Java.Lang.Object[])intent.Extras.Get("pdus");

            foreach(var item in smsArray)
            {
                var sms = SmsMessage.CreateFromPdu((byte[])item);

                SendReceivedStatus(sms.OriginatingAddress, sms.MessageBody);
            }
        }
    }

SendReceivedStatus(sms.OriginatingAddress,sms.MessageBody);

这是我的方法,转换为json receipentNumber和消息。

现在发送短信代码:

var sent = PendingIntent.GetBroadcast(Application.Context, 0, new Intent("SMS_SENT"), 0);

SmsManager.Default.SendTextMessage(responseModel.receipent, null, responseModel.message, sent, null);

protected override void OnResume()
    {
        base.OnResume();

        var smsSentReceiver = new SMSSentReceiver();

        RegisterReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));
    }

public override void OnReceive(Context context, Intent intent)
    {
        switch ((int)ResultCode)
        {
            case (int)Result.Ok:

                SenderActivity.resultsms = true;
                SendStatus();

                break;
            case (int)SmsResultError.GenericFailure:

                SenderActivity.resultsms = false;
                SendStatus();

                break;
            case (int)SmsResultError.NoService:

                SenderActivity.resultsms = false;
                SendStatus();

                break;
            case (int)SmsResultError.NullPdu:

                SenderActivity.resultsms = false;
                SendStatus();

                break;
            case (int)SmsResultError.RadioOff:

                SenderActivity.resultsms = false;
                SendStatus();

                break;
        }
    }

如何打包这些讯息?

1 个答案:

答案 0 :(得分:2)

由于自Android API级别19以来已弃用SmsMessage.CreateFromPdu((byte[])),因此我在此处提供了在KitKat版本之后接收超过160个字符的消息的方法,这是我的演示:

[BroadcastReceiver(Enabled = true, Label = "SMS Receiver")]
[IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" })]
public class SMSReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Bundle bundle = intent.Extras;
        if (bundle != null)
        {
            if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
            {
                SmsMessage[] msgs = Telephony.Sms.Intents.GetMessagesFromIntent(intent);
                var smstext = new StringBuilder();
                foreach (var msg in msgs)
                {
                    smstext.Append(msg.DisplayMessageBody.ToString());
                }
                Console.WriteLine(smstext.ToString());//output the received sms
            }
            else
            {
                var smsArray = (Java.Lang.Object[])bundle.Get("pdus");
                SmsMessage[] messages = new SmsMessage[smsArray.Length];
                for (int i = 0; i < smsArray.Length; i++)
                {
                    messages[i] = SmsMessage.CreateFromPdu((byte[])smsArray[i]);
                }
                StringBuilder content = new StringBuilder();
                if (messages.Length > 0)
                {
                    foreach (var message in messages)
                    {
                        content.Append(message.DisplayMessageBody.ToString());
                    }
                }
                Console.WriteLine(content.ToString());//output the received sms
            }
        }
        Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
    }
}

我已经在Android 6.0设备上测试了我的演示,它工作正常,我现在无法找到Android 4.0设备进行测试,但我认为该方法应该可以在这里工作。有关此问题的任何问题,请发表评论。