dimanche 10 mai 2015

Android: Delivered SMS pendingIntent

This is my code for sending SMS messages in my Android application:

private void SendSMS(final String message,final String phoneNumber)
{
    SmsManager sms = SmsManager.getDefault();
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
    registerReceiver(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;
            default: 
                break;
            }
        }
    }, new IntentFilter(Constants.SENT));
    registerReceiver(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;                        
            }
        }
    }, new IntentFilter(DELIVERED));        
    try{
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    }catch(Exception e){
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), EXCEPTION, Toast.LENGTH_LONG).show();
    }
}

Everything works fine, I can send SMS and both my broadcast receivers are triggered when the sms is sent and delivered. In my country every confirmation of delivered message has to be paid and even if on my device I can send a lot of messages for free, my credit decreases. There is some settings that I have missed and that I have to set to avoid delivered message confirmatin or I have to delete the "delivered" pendingIntent if actually it's the problem? Could you give me more information? Thanks

Aucun commentaire:

Enregistrer un commentaire