Implementation of simple SMS verification function on Android

Implementation of simple SMS verification function on Android

I believe that many friends will add the function of SMS verification when doing three-party login. Recently, I was assigned to realize this requirement. I am a novice and searched for information on the Internet. Currently, the more commonly used ones are Dahan.Cloud Communication and MOB's SMSsdk. After comparison, I finally chose the completely free MOB product to try my hand. The following are some of my experiences in the process of using the SDK. I hope that the masters who see it can give some suggestions. If you like it, just give a little heart.

First, I searched on Baidu for Mob's official website (http://www.mob.com/). As a first-time user, I opened the document introduction first. The introduction here is quite detailed about the usage, so I won't go into details here. I browsed the entire document and found that there are two methods, one is maven integration of AS, and the other is ordinary integration of Eclipse. The company's project is AS, so the latter is not considered. The maven integration method is really suitable for novices. It only needs to be configured in gradle, which saves time and effort. Here is the code first:

Add the following script to your root module build.gradle:

  1. buildscript {
  2. // Add the Maven address of MobSDK
  3. repositories {
  4. Maven {
  5. url "http://mvn.mob.com/android"  
  6. }
  7. }
  8. dependencies {
  9. // Register MobSDK
  10. classpath "com.mob.sdk:MobSDK:+"  
  11. }
  12. }
  13. In the build.gradle file under app, add the MobSDK plugin and extension, such as:
  14. // Add plugin
  15. apply plugin: "com.mob.sdk"  
  16. //Register SMSSDK related information in the MobSDK extension
  17. MobSDK
  18. appKey "d580ad56****"  
  19. appSecret "7fcae59a62342e7e2759e9e397**"  
  20. SMSSDK
  21. }
  22. }

//The appkey and appsecret here are obtained when creating an application in the mob background, so when you need to use it, go to the official website to register an account and enter the background to create

After configuring the above gradle configuration, the smssdk is basically integrated. The calling code is provided in the document. I copied and pasted it directly into my demo and tested it. It was unexpectedly smooth, but the only dissatisfaction is that this SMS verification interface is too old-fashioned. The product will definitely not pass it (put away the lazy thoughts). I silently studied the document again and saw a GUI-free usage method (http://wiki.mob.com/sms-android-%E6%97%A0gui%E6%8E%A5%E5%8F%A3%E8%B0%83%E7%94%A8/). They provide relevant interfaces. I will not post the specific pictures. You can go to the official website document to see it. It should be noted that if it is your own UI, you need to add it in the place where the gradle configuration of smssdk was previously configured.

  1. SMSSDK
  2. gui false  
  3. }

Because it's just a demo test, the UI layout is written casually, just to meet normal testing, it's a bit ugly

The following is the call to write the interface code. The document provides an initialization interface. RegisterEventHandler is used to register an event receiver in SMSSDK. SMSSDK allows developers to register any number of receivers. All receivers will receive messages when the event is triggered. The following is part of my code:

  1. EventHandler handler = new EventHandler(){
  2. @Override
  3. public void afterEvent( int event, int result, Object data) {
  4. if (result == SMSSDK.RESULT_COMPLETE){
  5.  
  6. //Callback completed
  7. if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
  8. //Submit verification code successfully
  9. runOnUiThread(new Runnable() {
  10. @Override
  11. public void run() {
  12. Toast.makeText(MainActivity.this, "Verification successful" , Toast.LENGTH_SHORT).show();
  13. }
  14. });
  15. } else if (event == SMSSDK.EVENT_GET_VOICE_VERIFICATION_CODE){
  16. runOnUiThread(new Runnable() {
  17. @Override
  18. public void run() {
  19. Toast.makeText(MainActivity.this, "Voice verification sent" , Toast.LENGTH_SHORT).show();
  20. }
  21. });
  22. }
  23. else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
  24. //Get verification code successfully
  25. runOnUiThread(new Runnable() {
  26. @Override
  27. public void run() {
  28. Toast.makeText(MainActivity.this, "Verification code has been sent" , Toast.LENGTH_SHORT).show();
  29. }
  30. });
  31. } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
  32. Log.i( "test" , "test" );
  33. }
  34. } else {
  35. ((Throwable)data).printStackTrace();
  36. Throwable throwable = (Throwable) data;
  37. throwable.printStackTrace();
  38. Log.i( "1234" ,throwable.toString());
  39. try {
  40. JSONObject obj = new JSONObject(throwable.getMessage());
  41. final String des = obj.optString( "detail" );
  42. if (!TextUtils.isEmpty(des)){
  43. runOnUiThread(new Runnable() {
  44. @Override
  45. public void run() {
  46. Toast.makeText(MainActivity.this,des,Toast.LENGTH_SHORT).show();
  47. }
  48. });
  49. }
  50. } catch (JSONException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. }
  55. };
  56. SMSSDK.registerEventHandler(handler);

The following is a simple call to the send verification interface:

  1. findViewById(R.id.tv_test1).setOnClickListener(new View .OnClickListener() {
  2. @Override
  3. public void onClick( View   view ) {
  4. phone = etVGetcode.getText().toString();
  5. //Get the verification code
  6. if (TextUtils.isEmpty(phone))
  7. Toast.makeText(MainActivity.this, "The number cannot be empty" , Toast.LENGTH_SHORT).show();
  8. Log.i( "1234" ,phone.toString());
  9. SMSSDK.getVerificationCode( "86" ,phone, null );
  10. }
  11. });
  12. findViewById(R.id.tv_test_vcode_valiable).setOnClickListener(new View .OnClickListener() {
  13. @Override
  14. public void onClick( View   view ) {
  15. //Submit verification code verification
  16. if (TextUtils.isEmpty(phone))
  17. Toast.makeText(MainActivity.this, "The number cannot be empty" , Toast.LENGTH_SHORT).show();
  18. number = etVCode.getText().toString();
  19. if (TextUtils.isEmpty(number))
  20. Toast.makeText(MainActivity.this, "The number cannot be empty" , Toast.LENGTH_SHORT).show();
  21. Log.i( "1234" ,phone+ "," +number);
  22. SMSSDK.submitVerificationCode( "86" ,phone,number);
  23. }
  24. });

The code is actually quite simple. The most important thing about the test is the verification efficiency. I personally feel it is quite fast. It is basically received within two or three seconds after sending. More importantly, it is free. It is still worth recommending. Having said so much, let’s take a look at the effect picture:

Supplementary explanation of a pit:

This is where you can add SMS product settings in the mob backend. I accidentally clicked it, which resulted in me not receiving any SMS messages. I had to ask the technical customer service for a long time before I found out. Please be careful when clicking.

The writing is rather crude, I hope it will be helpful to students in need =. =

<<:  Roll up your sleeves and write an Android universal refresh control

>>:  All the fun and useful apps from the App Store in the past ten years are here

Recommend

Analysis of Pop Mart’s Private Domain Operations

The blind box craze has made Pop Mart popular, ma...

The marketing secrets behind Perfect Diary’s explosive success

Seizing the opportunity of the rise of domestic t...

User operation: operation skills and channel establishment for user feedback

In operational work, there is a position that req...

Developers are pleased: Apple Watch software performed well on Christmas

This is a good sign for 2016. What apps do you us...