Android development: Your SMS Verification Code (1), android Verification Code

Source: Internet
Author: User

Android development: Your SMS Verification Code (1), android Verification Code

No fly, no sound, no sound. --------- Sima Qian


There are new requirements in recent work, requiring users to pass the mobile phone verification code during registration. The purpose of this is to prevent users from casually registering through an email address, today, let's take a look at the text message verification code in Android. If you have any mistakes, please criticize and correct them. If you have any questions, please leave a message. Thank you.


Before talking about this knowledge point, let's first understand how to aggregate data.

1. Introduction to aggregate data

Aggregate data is one of the largest providers of basic data APIs in China and specializes in Internet data services. Free data is provided from weather queries, air quality, map coordinates to financial funds, e-commerce price comparisons, illegal queries, and other fields for security, stability, and efficiency. Developers can use aggregated data APIs for quick development of mobile apps free of charge, eliminating data collection, maintenance, and other links, greatly reducing the development cycle and cost. Therefore, we can use aggregated data to provide me with a set of good things to add the SMS verification code function.

First, we need to make preparations. The following steps are used to aggregate data:

Step 1: Go to the aggregate data official website to register an account

After registration, go to "personal Center" --> click Apply for data, as shown in


Enter your mobile phone number, then you will receive the verification code, enter the verification code you received in the verification code, and click Apply

Step 2: After the application, you will see an Appkey. If you have already applied for an Appkey, follow these steps to view the Appkey you applied.

1. Go to the personal center and click my data, as shown in



2. Click the View button to view the applied Appkey. Therefore, this Appkey is very important.



The above two steps have completed the Appkey application, and then we proceed to step 3, download the SMS Verification Code SDK

Step 3 download the SMS Verification Code SDK

1. Go to the homepage and click the data interface, as shown in




2. Find the SDK center in the left-side category, as shown in



3. Click the SMS Verification Code SDK to go to the interface shown in


Here we have downloaded the SDK. Next we should go to step 4 to create the project configuration environment.

Step 4: Create a project and configure the environment

1. Create a project to copy the "armeabi file" and "smscaptcha_v_1_4.jar" in the downloaded SDK to the libs directory of our project.

2. Add development keys, required permissions, and other information to AndroidManifest.

(1) Add a development Key to the application

<Meta-data android: name = "JUHE_KEY" android: value = "Developer key"/>

(2) Add required permissions

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  <uses-permission android:name="android.permission.INTERNET" />  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION " />  <uses-permission android:name="android.permission.READ_FINE_LOCATION" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.READ_CONTACTS" />
(3) initialize the Context global variable referenced by the SDK during application Creation

In this step, we have two methods

First, configure in Activity

Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // initialize context information before using SDK components, input ApplicationContext // note that this method must be implemented before the setContentView method./*** Initialization Method * @ param context * @ needFriends requires friend function */CommonFun. initialize (getApplicationContext (), true); setContentView (R. layout. activity_main );}}

The following are the suggestions from the aggregated data document. We recommend that you put the initialization work in the Application, because the program will first run the method in the Application.

Note:You must call CommonFun. initialize (getApplicationContext (), true) before using each function component of the SDK. Therefore, we recommend that you put this method in the initialization method of the Application.

Therefore, we should follow the recommended method instead of using the first method. In this case, we need to create an Applicaiton class MyApplication as follows:

package com.example.android.sms;import com.thinkland.sdk.util.CommonFun;import android.app.Application;public class MyApplication extends Application {@Overridepublic void onCreate() {super.onCreate();CommonFun.initialize(getApplicationContext(), false); }}

After declaring the Application, do not forget to configure it in the configuration file.



The environment has been configured. Let's verify if we can receive the text message.

The MainActivity code is as follows:

Package com. example. android. sms; import com. thinkland. sdk. sms. SMSCaptcha; import com. thinkland. sdk. util. baseData; import com. thinkland. sdk. util. commonFun; import android. OS. bundle; import android. app. activity; import android. util. log; import android. view. menu; import android. widget. toast; public class MainActivity extends Activity {protected static final String TAG = "MainActivity"; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // initialize the SMSCaptcha object. In this object, SMSCaptcha smsCaptcha SMSCaptcha = SMSCaptcha. getInstance (); // call the method for sending the SMS Verification Code. There is a callBack/** phone number callBack return result callBack method. */smsCaptcha. sendCaptcha ("Enter your mobile phone number here", new BaseData. resultCallBack () {@ Overridepublic void onResult (int code, String reason, String result) {/* code: return code: SERVER: 0 success; 1 error; Local: -2 local network exception;-3 server network exception;-4 Resolution error;-5 initialization exception reason: returned message success or error cause. result: returned result in JSON format. error or no return value. */if (code = 0) {Log. I (TAG, "code =" + code); Log. I (TAG, "reason =" + reason); Log. I (TAG, "result =" + result );}}});}}


The returned results are as follows:


And the mobile phone received the text message. Well, this article is here. This article mainly sets up the environment. The next article will discuss with you the implementation of the UI, verify that the entered verification code is correct.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.