自动检测OTP问题

时间:2018-06-06 06:32:58

标签: android

我有一个模块可以在登录后自动检测OTP。在SAMSUNG中自动检测OTP,MOTOROLA手机在MI手机中正常工作时无法正常工作。如何解决这个问题呢?请帮我解决这个问题。感谢

1 个答案:

答案 0 :(得分:0)

我已经通过更改SMS Receiver Class解决了这个问题,现在它在所有提到的设备中都能正常工作。

这是班级:

公共类IncomingSms扩展了BroadcastReceiver {

// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {

    // Retrieves a map of extended data from the intent.
    final Bundle bundle = intent.getExtras();

    try {

        if (bundle != null) {

            final Object[] pdusObj = (Object[]) bundle.get("pdus");

            for (int i = 0; i < pdusObj.length; i++) {

                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                String senderNum = phoneNumber;
                String message = currentMessage.getDisplayMessageBody().replaceAll("\\D", "");

                //message = message.substring(0, message.length()-1);
                Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);

                Intent myIntent = new Intent("otp");
                myIntent.putExtra("message", message);
                myIntent.putExtra("number", senderNum);
                LocalBroadcastManager.getInstance(context).sendBroadcast(myIntent);
                // Show Alert

            } // end for loop
        } // bundle is null

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" + e);

    }
}

}