发送许多消息时发送SMS的待定意图无法正常工作 [英] Pending Intents for sending SMS not working well while sending many messages

查看:138
本文介绍了发送许多消息时发送SMS的待定意图无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个App,在其中发送SMS,我需要确定它是否已交付. 如果我向某人发送消息,一切似乎都很好.

I am Developing an App , In which I send SMS and I need to find if It's delivered or not. Every thing seems to be well if I send A message to a person.

但是在某些情况下,我得到了错误的信息,例如,首先我向A 0000发送一条消息(但不会发送),然后我向N0001发送一条消息并发送(到0000的消息仍然不发送)已传送),但我举杯同庆:sms已传送至0000(但仅传送了至0001的消息),我该怎么办才能解决Delivery报告中的此冲突?

But In some situations I got wrong Information , for example , First I send A message to number 0000 (But It will not deliver) and after it I send a message to number 0001 and It delivers (message to 0000 is still not delivered) but I got a toast with : sms delivered to 0000(But only message to 0001 is delivered) , What Should I do to fix this conflict in Delivery reports ?

这是我的代码:

try {
    SmsManager smsManager = SmsManager.getDefault();

    String to = "5556";
    String body = "Test Message";

    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED).putExtra("senderNumber", to), 0);

    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(arg0, "SMS sent", Toast.LENGTH_LONG).show();
                    break;
                default:
                    Toast.makeText(arg0, "Error", Toast.LENGTH_LONG).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    String s = arg1.getStringExtra("senderNumber");
                    Toast.makeText(getBaseContext(), "SMS delivered to " + s, Toast.LENGTH_LONG).show();
                    break;
                default:
                    Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_LONG).show();
                    break;                        
            }
        }
    }, new IntentFilter(DELIVERED));        

    smsManager.sendTextMessage(to.getText().toString(), null, body.getText().toString(), sentPI, deliveredPI);
}
catch (Exception ex) {
    Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}

更新:

我发现,如果我将一个唯一的ID添加到Intent动作(DELIVERED + id)中,那将是没有问题的. 但是我没有在创建消息时注册接收方,而是使用清单文件来做到这一点:

I find that If I add a unique id to Intent action (DELIVERED+id) It will be fine and no conflict. but I don't register the Receiver on creating message , I use manifest file to do this:

<receiver android:name=".SendBroadcastReceiver" >
    <intent-filter>
        <action android:name="com.example.myapp.SMS_SENT" />
        <action android:name="com.example.myapp.SMS_DELIVERED" />
    </intent-filter>
</receiver> 

还有一个名为SendBroadcastReceiver的Receiver类,用于处理短信传递.

And also a Receiver class named SendBroadcastReceiver to handle sms delivery.

如果我将唯一的ID添加到action,如何将它们添加到清单文件中?

If I add a unique id to action , how can I add them in manifest file?

推荐答案

似乎您没有在清单中添加广播接收器.您已经匿名声明了广播接收者,因此首先需要为广播接收者声明一个名为sendPI和deliveryPI的名称

It seems like you have not added your broadcast receivers in manifest. You have declared your broadcast receivers anonymously, so first of all you need to declare your broadcast receivers with some name for sendPI and deliveredPI

这篇关于发送许多消息时发送SMS的待定意图无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆