0

I am trying to auto fetch otp sms and fill it in my otp dialog box but this is not working in android 8.0. Here's my sms recevier class :

public class SMSBroadCastReceiver extends BroadcastReceiver {

    public static SmsListener listener = null;
    public static void setListener(SmsListener listener) {
        SMSBroadCastReceiver.listener = listener;
    }
    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();
        SmsMessage[] sms=null;

        String smsStr = "";
        if(bundle!=null)
        {
            Object[] pdus = (Object[]) bundle.get("pdus");
            sms = new SmsMessage[pdus.length];
            for(int i=0;i<sms.length;++i){
                sms[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                smsStr +=sms[i].getDisplayMessageBody().toString();
                String Sender = sms[i].getOriginatingAddress();
                Intent smsIntent = new Intent("otp");
                smsIntent.putExtra("message",smsStr);

                LocalBroadcastManager.getInstance(context).sendBroadcast(smsIntent);
                if(Sender.contains("OTPSMS")){
                    listener.onMessageRecieved(smsStr,Sender);
                }
            }
        }
    }

Dialog box is opening but it is not auto filling by itself. Here's I am opening my OTP dialog box :

public static void openOtpDialog(final String userCredentials, final Context context, final Boolean isPhoneType, final Boolean isSignIn, final Boolean isBooking, final String userName) {
               Log.e("InsideOtpDialog","yes");
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        OtpDialogView = inflater.inflate(R.layout.otp_dialog, null);
        dialogBuilder.setView(OtpDialogView);
        dialogBuilder.setCancelable(true);


        OtpOne=(EditText) OtpDialogView.findViewById(R.id.otp1);
        OtpTwo=(EditText) OtpDialogView.findViewById(R.id.otp2);
        OtpThree=(EditText) OtpDialogView.findViewById(R.id.otp3);
        OtpFour=(EditText) OtpDialogView.findViewById(R.id.otp4);
        TextView resendButton=(TextView) OtpDialogView.findViewById(R.id.resend);

        final AlertDialog alertDialog = dialogBuilder.create();

        alertDialog.setCanceledOnTouchOutside(true);
        alertDialog.show();

        resendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Constants.closeDialog(alertDialog);
                sendOtpToUser(userCredentials,context,isPhoneType,isSignIn,"user",isBooking,userName);
            }
        });


        OtpOne.addTextChangedListener(new FocusSwitchingTextWatcher(OtpTwo));
        OtpTwo.addTextChangedListener(new FocusSwitchingTextWatcher(OtpThree));
        OtpThree.addTextChangedListener(new FocusSwitchingTextWatcher(OtpFour));
        OtpFour.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                if (OtpFour.getText().toString().length()==1){

                    Constants.closeDialog(alertDialog);
                    String otp=OtpOne.getText().toString()+OtpTwo.getText().toString()+OtpThree.getText().toString()+OtpFour.getText().toString();
                    verifyOtp(otp,userCredentials,context,isSignIn,isBooking);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
      /*  BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

            }
        };*/
              SMSBroadCastReceiver.setListener(new SmsListener() {
                  @Override
                  public void onMessageRecieved(String msg, String sender) {
                      Log.e("MessagingDevice", msg);
                    //  String [] messgae = msg.split("OTP -");
                      String otp1 = msg.split(": ")[1];
                         char[] otp =   otp1.toCharArray();
                         Log.e("OTP is",otp[0] +"");
                      OtpOne.setText(otp[0]+"");
                      OtpTwo.setText(otp[1]+"");
                      OtpThree.setText(otp[2]+"");
                      OtpFour.setText(otp[3]+"");
                  }
              });
    }

Any suggestions how can I do it. It is working fine in all devices except android 8.0 devices.

Here's My android Manisfest file :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myname">
 <uses-permission android:name="android.permission.SEND_SMS" />
  <application
           <receiver android:name=".HelperClass.SMSBroadCastReceiver">
            <intent-filter android:priority="999">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
</application>
Steve Vinoski
  • 19,847
  • 3
  • 31
  • 46
Logan
  • 101
  • 1
  • 2
  • 12

1 Answers1

0

As there is no Answer to this question and I also searched this before. here is the solution I have written previously. Use this Library Link. You will not have to mess with anything. After adding dependency simply use this method.

OtpFetcher.getInstance().verifyOtpByMatchingString(this, "OTP", 21000, object : OtpListener {
            override fun onReceived(messageItem: MessageItem) {
                Toast.makeText(this@MainActivity, "" + messageItem, Toast.LENGTH_SHORT).show()
            }

            override fun onTimeOut() {
                Toast.makeText(this@MainActivity, "TimeOut", Toast.LENGTH_SHORT).show()

            }
        })

You will have to pass Context, Search String of your message for Example

You are expecting OTP in your message Pass "OTP" and Time Out for How long you want to listen OTP and That's It. You will get your message in a simple format in OnRecieved CallBack.

Intsab Haider
  • 3,491
  • 3
  • 23
  • 32