Pending Intents for sending SMS not working well while sending many messages

丶灬走出姿态 提交于 2019-12-07 07:25:18

问题


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.

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 ?

Here is my code:

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();
}

Update:

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> 

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

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


回答1:


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



来源:https://stackoverflow.com/questions/24996638/pending-intents-for-sending-sms-not-working-well-while-sending-many-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!