Notify if a message you sent was sent successfully or not in Android

前端 未结 2 1710
故里飘歌
故里飘歌 2021-01-16 03:20

I have some code here to send a message:

SmsManager sms = new SmsManager.getDefault();
sms.sendTextMessage("911", null, "HALP!", PendingIn         


        
相关标签:
2条回答
  • 2021-01-16 03:25

    Try this...

                PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    
                    registerReceiver(new BroadcastReceiver() 
                    { int resultCode = getResultCode();
                            switch (resultCode) 
                            {
    
                            case Activity.RESULT_OK:                 
    
                                Toast.makeText(getBaseContext(), "SMS sent"+message,Toast.LENGTH_SHORT).show();
    
                            break;
    
                            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:  
    
                                Toast.makeText(getBaseContext(), "Generic failure",Toast.LENGTH_SHORT).show();
                            break;
    
                            case SmsManager.RESULT_ERROR_NO_SERVICE:
    
                                Toast.makeText(getBaseContext(), "No service"+message,Toast.LENGTH_SHORT).show();
                            break;
    
                            case SmsManager.RESULT_ERROR_NULL_PDU:     
    
                                Toast.makeText(getBaseContext(), "Null PDU",Toast.LENGTH_SHORT).show();
                            break;
    
                            case SmsManager.RESULT_ERROR_RADIO_OFF:  
    
                                Toast.makeText(getBaseContext(), "Radio off",Toast.LENGTH_SHORT).show();
                            break;
                            }
                        }
                    }, new IntentFilter(SENT));
    
    
                    SmsManager smsMgr = SmsManager.getDefault();
                    smsMgr.sendTextMessage(send_no, null,message, sentPI, null);
    
    0 讨论(0)
  • 2021-01-16 03:44

    This can be achieve simply by registering your PendingIntent with BroadcastReceiver that notify you when your Message is SEND and when it DELIVERED to intended recipient. Just follow below steps_

    1. Build two PendingIntent one for send and other for deliver notification.
    2. Build two BroadcastReceiver. One for send and another for deliver.
    3. Finally, register these PendingIntent with respective BroadcastReceiver through registerReceiver, So that their BroadcastReceiver get notified and do the needfull.

    I have build one simple class that do the work that i have explained above. You may go through this class to have complete understanding of it_

    public class SendDeliverSMSActivity extends Activity {
    Button btnSendSMS;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.main);
        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
        btnSendSMS.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Your method that send sms
                sendSMS("5556", "Hi You got a message!");
            }
        });
    
    }
    
    /**
     * ---sends an SMS message to another device---
     * 
     * @param phoneNumber
     * @param message
     */
    private void sendSMS(String phoneNumber, String message) {
        // Intent Filter Tags for SMS SEND and DELIVER
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";
    // STEP-1___
        // SEND PendingIntent
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
                SENT), 0);
    
        // DELIVER PendingIntent
        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                new Intent(DELIVERED), 0);
    // STEP-2___
        // SEND BroadcastReceiver
        BroadcastReceiver sendSMS = new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        };
    
        // DELIVERY BroadcastReceiver
        BroadcastReceiver deliverSMS = new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        };
    // STEP-3___
        // ---Notify when the SMS has been sent---
        registerReceiver(sendSMS, new IntentFilter(SENT));
    
        // ---Notify when the SMS has been delivered---
        registerReceiver(deliverSMS, new IntentFilter(DELIVERED));
    
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
     }
    }
    

    I hope this will help you! :)

    0 讨论(0)
提交回复
热议问题