Android 8.0中的OTP自动提取

时间:2018-02-28 05:53:56

标签: android broadcastreceiver one-time-password

我正在尝试自动获取otp短信并将其填入我的otp对话框中,但这在Android 8.0中无效。  这是我的短信接收课程:

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);
                }
            }
        }
    }

对话框正在打开,但它不会自动填充。这是我打开我的OTP对话框:

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]+"");
                  }
              });
    }

任何建议我该怎么办?它在除android 8.0设备之外的所有设备中都能正常工作。

这是我的Android Manisfest文件:

<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>

1 个答案:

答案 0 :(得分:0)

因为这个问题没有答案,我之前也搜索过。这是我之前写的解决方案。 使用此库 Link。您将不必搞砸任何事情。 添加依赖后只需使用此方法。

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()

            }
        })

您必须传递上下文,例如您的消息的搜索字符串

<块引用>

您在您的消息中期待 OTP 通过“OTP”和超时您想收听 OTP 的时间,仅此而已。您将在 OnRecieved CallBack 中以简单的格式收到您的消息。