Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback Listener always returning null

I am trying to create a simple SMS library for Android for which i have created some custom listener. I want to trigger that listener whenever an event occurs eg. SMS is sent or gets failed.I am able to send and receive the SMS but the listener events aren't getting called coz my listener is always returning Null. Can anybody tell me where am i doing wrong?

public class SendSMS {

    private  OnSMSListener listener;

    public void OnSMSListener(OnSMSListener listener) {
        this.listener = listener;
    }

    public SendSMS(Context context) {
        Holder.setContext(context);
        this.listener = null;

    }


    public void setNumber(String number)  {
        Holder.setMobNumber(number);

    }

    public void setMessage(String message) {
        Holder.setMessage(message);
    }

    public void sendMessage() {
        ImplSendSms.sendTextMessage(listener);
    }

    /**
     * Interface definition for a callback to be invoked
     */
    public interface OnSMSListener {

        void onSmsSent();

        void onGenericFailure();

        void onNoService();

        void onNullPdu();

        void onRadioOff();
    }

MyImplSendSms.class File:

public class ImplSendSms {

    public  static void sendTextMessage(final SendSMS.OnSMSListener listener) {

        Toast.makeText(Holder.getContext(), "sendTextMessage", Toast.LENGTH_SHORT).show();

        String SENT = "SMS_SENT";
        PendingIntent sentPI = PendingIntent.getBroadcast(Holder.getContext(), 0,
                new Intent(SENT), 0);

//---when the SMS has been sent---
        Holder.getContext().registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(Holder.getContext(), "Inside Sent", Toast.LENGTH_SHORT).show();
                        if (listener != null) {
                            Toast.makeText(Holder.getContext(), "Inside Sent Listener", Toast.LENGTH_SHORT).show();
                            listener.onSmsSent();
                        }

                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        if (listener != null) {
                            listener.onGenericFailure();
                        }

                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        if (listener != null) {
                            listener.onNoService();
                        }
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        if (listener != null) {
                            listener.onNullPdu();
                        }
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        if (listener != null) {
                            listener.onRadioOff();
                        }

                        break;
                }
            }
        }, new IntentFilter(SENT));


        SmsManager sms = SmsManager.getDefault();


        sms.sendTextMessage(Holder.getMobNumber(), null, Holder.getMessage(), sentPI, null);
    }

My MainActivity

SendSMS sms = new SendSMS(MainActivity.this);
        sms.setMessage("Hello");
        sms.setNumber("123456789");
        sms.sendMessage();
        sms.OnSMSListener(new SendSMS.OnSMSListener() {
            @Override
            public void onSmsSent() {
                Toast.makeText(MainActivity.this, "SMS Sent", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onGenericFailure() {
                Toast.makeText(MainActivity.this, "Generic Failure", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNoService() {
                Toast.makeText(MainActivity.this, "No Service", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNullPdu() {
                Toast.makeText(MainActivity.this, "Null PDU", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRadioOff() {
                Toast.makeText(MainActivity.this, "Radio Off", Toast.LENGTH_SHORT).show();
            }
        });
like image 378
Aamir Avatar asked Feb 28 '26 02:02

Aamir


1 Answers

You are setting the listener after you call sendMessage(). Change to the following:

    sms.OnSMSListener(new SendSMS.OnSMSListener() {
        @Override
        public void onSmsSent() {
            Toast.makeText(MainActivity.this, "SMS Sent", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onGenericFailure() {
            Toast.makeText(MainActivity.this, "Generic Failure", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNoService() {
            Toast.makeText(MainActivity.this, "No Service", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNullPdu() {
            Toast.makeText(MainActivity.this, "Null PDU", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onRadioOff() {
            Toast.makeText(MainActivity.this, "Radio Off", Toast.LENGTH_SHORT).show();
        }
    });
    sms.sendMessage();

PS. Your OnSMSListener should be setOnSMSListener().

like image 56
StuStirling Avatar answered Mar 01 '26 17:03

StuStirling