Android realizes the automatic filling function of SMS verification code

Android applications often involve the registration and login function, and many registration and login or password modification functions often need to enter the SMS verification code. Generally, users need to minimize the application to view the SMS and fill in the verification code after receiving the SMS, which is bound to be troublesome. Therefore, it is necessary to automatically obtain the SMS verification code issued, which facilitates the user's operation and better user experience.

Principle explanation:

It is mainly to obtain SMS information in real time. It involves the use of contentobserver class. Use ContentProvider to monitor the changes of SMS database, implement onchange method in the customized contentobserver to monitor the SMS of specific mobile phone number, and then intercept the information to fill in the position to be filled. Contentobserver is the content listener. When we send a short message to the mobile phone, the mobile phone will automatically call the specified method in contentobserver to notify that the short message has changed. Then we read the content in the short message, extract the verification code and automatically fill it in the input box, so as to complete the automatic filling function. Contentobserver class mainly monitors the changes of SMS content. Here, it involves a common design mode of Android, namely observer mode.

Contentobserver explanation - observer mode:

Observer mode (sometimes called publish subscribe mode, model view mode, source listener mode or dependent mode) is a kind of software design mode. In this mode, a target object manages all observer objects that depend on it and actively notifies when its own state changes. This is usually achieved by calling the methods provided by each observer. This pattern is often used to implement event processing systems. Observer mode perfectly separates the observer from the observed object. The observer pattern draws clear boundaries between modules and improves the maintainability and reusability of applications. Observer design pattern defines a one to many dependency between objects, so that when the state of an object changes, all objects that depend on it are notified and refreshed automatically.

Contentobserver - a content observer, whose purpose is to observe (capture) the changes in the database caused by a specific URI, and then do some corresponding processing. It is similar to the trigger in database technology. When the URI observed by contentobserver changes, it will be triggered. • Observer (i.e. our application): observer registers itself in the subject, and the subject stores the observer in a container. • Observed (i.e. SMS application of the system): if the observed object has changed (such as somechange in the figure), get all registered observers from the container and notify the observers of the change. • Withdraw observation: the observer tells the observed to withdraw the observation, and the observed removes the observer from the container.

Specifically in our project, that is, when the application starts running, an observer will be registered with the SMS application of our mobile phone system. When the SMS changes, the SMS application will notify the registered observer of the change. When our observer receives such a notification, he will perform corresponding operations according to the code, So as to realize the function of automatically filling in the verification code. When we complete the required functions, we need to revoke the observation, cancel the registration, and remove the observer from the container. After the observer is revoked, he will no longer receive the content change notice of the SMS.

The steps to observe a specific URI are as follows:

1. To create our specific contentobserver derived class, we must overload the parent class construction method and the onchange () method to handle the function implementation after callback. 2. Use context. Getcontentrestolover() to obtain the contentrestolove object, and then call the registercontentobserver() method to register the content observer. 3. Because the life cycle of contentobserver is not synchronized with activities and services, you need to manually call unregistercontentobserver() to cancel registration when it is not needed.

activity_ main.xml

MainActivity.java

SmsObserver.java

There are the following URI types of SMS:

content://sms/in @R_ 521_ 2419 @ inbox content://sms/sent Sent content://sms/draft Draft content://sms/out @R_ 521_ 2419 @ outbox (message being sent) content://sms/Failed Send failed content://sms/queued List to be sent (for example, after the flight mode is turned on, the message will be in the list to be sent)

Of course, don't forget to add the permission to read SMS: < uses permission Android: name = "Android. Permission. Read_sms" / >

Source download: Android SMS verification code is automatically filled in

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
分享
二维码
< <上一篇
下一篇>>