Quickly develop mobile phone number registration and login function in three minutes

        Mobile phone number registration and login is now a must-have function for most apps. Through the verification of the verification code, we have obtained the user's mobile phone number, so as to ensure security and generate value in the following aspects:

  1. It meets the needs of national information security supervision and prevents individual users from violating regulatory requirements, and we cannot trace the source.
  2. In line with the needs of product operations, with the user's mobile phone number, we can better carry out operations and marketing activities, and build private domain traffic for our products.

        Having said so much, how should we develop the mobile phone number registration and login function in a short period of time?

        Let’s take a look at the final effect of the program:

1. Related tools

Android Studio Flamingo 2022.2.1版

2. Obtain SMS channel service provider

        There are many SMS verification code channel service providers in China, but many channel providers either have weak technical strength and directly throw an http document to you, and the request is still in the form of an IP address, or use a mixed channel, and the sent SMS cannot be received for a long time If there is no technical customer service to keep in touch, it is difficult to find a docking person if there is a problem.

        Here I choose Bmob backend cloud as our SMS channel service provider, which is an old-fashioned cloud service provider, stable and reliable.

        Go to the Bmob backend cloud official website , register and log in, create an application, and add a custom template under this application.

Click the save button and wait for the official review. During working hours, the speed is very fast.

At the same time, click the Settings button to get the Application ID of your application.

3. Development process

        Everything is ready, we can officially start the development.

  • Create an Android project

        Here I choose the Java language as the project language.

        Create the App class

        This App class inherits from the Application class, and the code inside is also very simple, just replace the Application ID with the one you just obtained.

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Bmob.initialize(this,"Your Application ID");
    }
}
  • Modify the AndroidManifest.xml file

        Add permissions at the same level of the Application node as follows:

<!--允许联网 -->
<uses-permission android:name="android.permission.INTERNET" />
<!--获取GSM(2g)、WCDMA(联通3g)等网络状态的信息  -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--允许读取手机状态 用于创建BmobInstallation-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
  • Add android:name to the application node, and change this value to the name of the newly created App class, as follows:
<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.Bmobandroiddemosms"
    tools:targetApi="31"
    android:name=".App">
  • Add a new provider in the application node, as follows:
<provider
    android:name="cn.bmob.v3.util.BmobContentProvider"
    android:authorities="这个改为你的App包名.BmobContentProvider">
</provider>
  • Send SMS verification code

After doing the above, you can officially access the SMS verification code in your business logic. code show as below:

BmobSMS.requestSMSCode(phone, "你刚刚填写的短信模板名称", new QueryListener<Integer>() {
    @Override
    public void done(Integer smsId, BmobException e) {
        if (e == null) {
            mTvInfo.append("发送验证码成功,短信ID:" + smsId + "\n");
        } else {
            mTvInfo.append("发送验证码失败:" + e.getErrorCode() + "-" + e.getMessage() + "\n");
        }
    }
});

Wherein, phone is a mobile phone number input by the user terminal.

  • Verify SMS verification code

The code to verify the SMS verification code is also very simple, only a few short lines below.

BmobSMS.verifySmsCode(phone, code, new UpdateListener() {
    @Override
    public void done(BmobException e) {
        if (e == null) {
            mTvInfo.append("验证码验证成功,您可以在此时进行重要操作!\n");
        } else {
            mTvInfo.append("验证码验证失败:" + e.getErrorCode() + "-" + e.getMessage() + "\n");
        }
    }
});

Among them, phone is the mobile phone number entered by the user, and code is the verification code received by the user.

4. Source code acquisition

Users who need it can directly contact me to obtain the source code. Welcome to exchange how to develop App faster.

Guess you like

Origin blog.csdn.net/m0_74037076/article/details/130880226