Android SMS verification code automatic filling implementation code

Today, I'd like to share with you a function of automatically filling in Android SMS verification code. First look at the effect picture. I sent a text message to my mobile phone and automatically filled in the verification code. As shown in the figure:

This little function applies to observer mode. What is observer mode?

Observer mode: defines a subject to observer dependency between objects. When the state of an object changes, the objects that depend on it are notified and automatically updated. In this example, we register an observer in the SMS. When the SMS function (observed) receives the information, it will notify our registered observer. The observer used this time is contentobserver, which is the database change caused by a specific URI.

Here are the steps to use the content Observer:

1. To create a derived class of contentobserver, the constructor of the parent class must be overloaded, and the onchange () method must be overloaded to handle the function implementation after the callback. 2. Register the content observer, use context. Getcontentrestolover() to obtain the contentrestolover object, and then call the registercontentobserver() method to register the content observer. 3. Because the life cycle of contentobserver is not synchronized with activity and service, you need to manually cancel the registration when it is not needed.

Let's look at the code:

1. XML of layout

2. Mainactivity is mainly used to register and revoke content observers

3. The most critical content observer

You can add a judgment before the regular expression, so that only the SMS sent by a specific number can be read.

Finally, add the permission to read SMS. Well, it's basically done. Finally, attach the source code: Android SMS verification code is automatically filled in

Thank you. If you have any questions, you can communicate below.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>