Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set OTP message on my EditText Automatically

Tags:

android

Everything works fine ,I also get otp on my device but the problem i face it doesnt set automatically on my editText i am new in android , here is my getSMSclass.

public class GetIncomingSms extends BroadcastReceiver
{
SmsMessage currentMessage;
final SmsManager sms = SmsManager.getDefault();

@Override
public void onReceive(Context context, Intent intent)
{
    if(intent.getAction() == "android.provider.Telephony.SMS_RECEIVED") {


        final Bundle bundle = intent.getExtras();

        try {
            if (bundle != null) {
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
                for (int i = 0; i < pdusObj.length; i++) {

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        String format = bundle.getString("format");
                        currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i], format);
                    } else {
                        currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    }

                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    try {
                        if (senderNum.equals("'myphonenumber'") ) {
                            Otp Sms = new Otp();
                            Sms.recivedSms(message);
                                                        }
                    } catch (Exception e) {
                    }
                }
            }
        } catch (Exception e) {
        }

    }
}

}

and my otp class where i want to set my sms on my editText

public void recivedSms(String message)
{
        System.out.println("Register Otp = "+message);
        otp.setText(message);

}
like image 852
andy02 Avatar asked Feb 27 '26 15:02

andy02


1 Answers

check this site here is example how to get otp automatically

http://androiddhina.blogspot.in/2015/06/reading-incoming-message-automatically-to-verify-OTP.html

and change your

 Otp Sms = new Otp();
 Sms.recivedSms(message);

to

Otp.recivedSms(message);

and also where you want to set otp on edittext please make sure it is static:

public void recivedSms(String message)
{
    System.out.println("Register Otp = "+message);
    otp.setText(message);
 }

to

public static void recivedSms(final String message)
{
    otp.setText(message);
}
like image 149
Mahesh Sehajpal Avatar answered Mar 02 '26 14:03

Mahesh Sehajpal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!