Implementation method of Android acquiring and reading SMS verification code

Nowadays, verification code is still very common in Android clients. Directly register the information of application account through mobile account and verification code. Many applications complete registration in this way. Let's briefly introduce it

Android is relatively simple to obtain the SMS verification code. You can obtain the SMS verification code by calling the internal method through the sharesdk provided by mob's official website. Please provide mob's official website address http://www.mob.com/#/ After registering the relevant information on the official website, download the relevant jar package and. So file to obtain the SMS verification code (before version 2.0, you need to download the jar package and. So file, but now version 2.2 does not need to download the. So file. You can use it directly by loading smssdk.jar, mobcommons.jar and mobtools.jar). I won't explain how to register

Finally, the registered style is like this.. let's see the specific implementation

1. How to obtain SMS verification code

i. First, you need to initialize the SDK. The first necessary operation for third parties is to initialize the SDK. Generally, it is completed in the oncreate() function

This is a necessary operation, otherwise the subsequent things will not be completed. Initsdk (context, string appkey, string appsecret), the initialization needs to pass the context object and the key and secret we applied for. In addition, an EventHandler is defined here to provide some messages to the handler of the main thread during verification, Let the main thread do some related operations to inform the user how the authentication is

II. Call the smssdk.getverificationcode (string, string) method

After initializing the SDK, we can get our verification code by using the getverificationcode () method

When we call the method, we need to pass the area code and specific mobile phone number of our mobile phone number. Since China starts with 86, the passed area code is 86. Adding our own phone number, we can obtain the relevant verification code through the network call method

III. verify that the verification code we entered is consistent with the verification code we sent

When the verification code is sent, the client usually needs to input it, but a verification process is required to judge whether the verification code entered by the current user is consistent with the verification code sent

SMSSDK.submitVerificationCode("86",phone,CodeEd.getText().toString()); The verification method is completed by calling the submittverificationcode() method. The area code, telephone number and the value of the verification code we entered need to be passed. The verification process is completed by sharesdk for us. Therefore, there is no need to perform too many complex operations. When the value we pass is the same as the value sent, the verification will succeed, otherwise the verification will fail

In this way, the registration function can be completed on our client software through this verification method. When the verification is successful, you can enter a new interface. If the verification fails, you need to confirm the entered verification code. In this way, you can complete the verification code verification of the application

Generally, we only need to check the SMS and submit the relevant verification code, but there are other applications that are more user-friendly. When the verification code information is sent to the inside of the mobile phone, we can directly obtain the relevant verification code, and then add it directly to the place where verification is needed, which is very convenient, It can also prevent user input errors. Then it involves reading the relevant contents of SMS

IV. add relevant permissions

So how to get the relevant content of SMS?

2. How to get the relevant content of the SMS just received

Generally speaking, SMS verification is directly sent to users in the form of new SMS. If the application wants to read the newly received SMS content, it needs to have relevant listening events. I implement it by using contentobserver. By using this class, you can capture a specific URI to change the database, and then do some related processing

Then we can implement it in this way. By inheriting the contentobserver class, rewriting the internal onchange method, and setting a specific URI, our class can monitor the change of SMS data, so that our application will know when the SMS arrives. After the SMS arrives, we obtain the SMS content, Then read the verification code information in the content

The implementation method of the class is as above. The onchange method is rewritten to perform subsequent operations. The cursor here can search the data in the current SMS database. The cursor pointer here does not use the while loop, because the verification code SMS is ready to use, and we only need to obtain the relevant contents in the currently sent verification code SMS, If cursor uses a while loop, it will read all the contents of the message. This is not what we want

After we get the specific content of the SMS, we can use regular expressions to match the numbers of the SMS content, and then we can get the verification code data. The general idea is this case. At the same time, we need to add relevant user permissions

After completing the above steps, we need to get the contentresolver instance and register the contentobserver.

getContentResolver().registerContentObserver(Uri.parse(" content://sms "),true,new SmsObserver(new Handler())); For registration, we need to pass the relevant URIs. The second parameter determines the way to match the URIs. If it is set to true, it means that it is not an exact match, which means that if it is a class of URIs, it will be matched. If it is set to false, it can only match the URIs we pass in, That is the so-called exact matching. The last object needs to pass an instance of a subclass and the handler object. In this way, we can update the UI in this method

When we don't need to use content observer, we just need to unregister

Relatively speaking, the content of verification code information is relatively small. If the content is complex and there are other additional digital information, we need to optimize the regular expression at the same time

Last source code:

In this way, we can complete a simple verification through the use of SMS verification code. In general projects, we can make relevant improvements according to specific needs. In short, everything changes. The idea is basically the same. Of course, we can also use broadcasereceiver to judge whether there are SMS, but I have seen some relevant resources on the Internet, I tried it myself and didn't realize it. I don't think it's as simple and convenient as contentobserver

Source code download: Android gets and reads SMS verification code

The above is the implementation method for Android to obtain and read SMS verification code. I hope it will be helpful for everyone to learn Android Software Programming.

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