Skip to content Skip to sidebar Skip to footer

Create Alert Dialog On Receiving Sms

I am creating a app for android which sends sms using gateways like 'way2sms.com', 'fullonsms.com', i have succeeded in sending messages from those gateways, now i want to take ano

Solution 1:

This code would cover all the possible cases of your SMS sending.

privatevoidsendSMS(String phoneNumber, String message)
{        
    StringSENT = "SMS_SENT";
    StringDELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
        newIntent(SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
        newIntent(DELIVERED), 0);

    //---when the SMS has been sent---registerReceiver(newBroadcastReceiver(){
        @OverridepublicvoidonReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                caseActivity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent", 
                            Toast.LENGTH_SHORT).show();
                    break;
                caseSmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", 
                            Toast.LENGTH_SHORT).show();
                    break;
                caseSmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service", 
                            Toast.LENGTH_SHORT).show();
                    break;
                caseSmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", 
                            Toast.LENGTH_SHORT).show();
                    break;
                caseSmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off", 
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, newIntentFilter(SENT));

    //---when the SMS has been delivered---registerReceiver(newBroadcastReceiver(){
        @OverridepublicvoidonReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                caseActivity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;
                caseActivity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;                        
            }
        }
    }, newIntentFilter(DELIVERED));        

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
}

And here is a great tutorial!

Solution 2:

I used this tutorial, when i was creating something similiar!

Just jump to Handle Received SMS!

Post a Comment for "Create Alert Dialog On Receiving Sms"