How to log in with mobile phone number in android (the latest version of 2020 ultra detailed Mob platform + SMSSDK)?


Although the mob platform can log in with a mobile phone verification code, there is a drawback 每天只能发送10条验证码。
建议仔细观看每一个步骤,如果一个步骤没处理好,可能就会让你的这个功能无法实现。相信我一定可以成功的。

If you want to realize the mobile push function through android, you can read this article. Android teaches you how to easily realize the mobile push function. Step by step, teach you Mob+MobPush to realize the push function?

The first step: register mob platform account

Because I have already registered for the mob platform and there is no extra mobile phone number, I won't elaborate on it. Registration is relatively simple. The link to the mob platform is attached below:
mob platform link : https://www.mob.com/

Step 2: Configure the SMSSDK environment on the mob platform.

1. Click the product center in the red circle in the figure below.

Insert picture description here

2. Click to enter now.

Insert picture description here

3. Click Create Application.

Insert picture description here

4. Agree to the privacy service.

Insert picture description here

5. Create an application.

Remember that the picture is set to 128px*128px
Insert picture description here
If you don’t know how 设置图片大小, you can search the FastStone editor on the Internet. I don’t know where to put his installation package. You can find it on the Internet, but the drawing software that comes with the window seems to be able to set it. The size (I am not very sure). Don't talk nonsense, get to the point.
Insert picture description here

6. Click the red circle in the figure below to access the interface.

Insert picture description here

7. Select SMSSDK.

Choose as shown in the SMSSDKpicture (I don’t know if you need to add this in the second test. I added it before, but it has no effect. Adding it is better than not adding it. Anyway, it doesn't get in the way).
Insert picture description here

8. Get your App Keysum App Secret.

It is recommended to use your own, because only 10 messages can be sent for each created application.
Insert picture description here

9. Click SDK to download.

as the picture shows
Insert picture description here

10. Download SMSSDK.

The steps are shown in the figure.
Insert picture description here
The download here is not through manual download, but through code download, so simple.
Insert picture description here

Step 3: Add SMSSDK code to android

1. Create a new project.

Insert picture description here

2. Add permissions in AndroidManifest.

As shown in the figure: the
Insert picture description here
permission code is as follows:

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

3. Add the code in the builder.grale of the Project.

As shown in the figure, the
Insert picture description here
code is as follows:

classpath "com.mob.sdk:MobSDK:2018.0319.1724"
maven {
    
     url 'https://jitpack.io' }

4. Add code to builder.gradle in Moudule

记得修改为你自己的apppKey和appSecret
If yours is android studio4.0,4.1to add the following code:
Insert picture description here

If you are in another version, just add the format below, as long as you modify it toapply plugin:'com.mob.sdk'
Insert picture description here

The code is as follows, select the code according to your version:
android studio4.0.4.1版本

id 'com.mob.sdk'

其他版本

apply plugin:'com.mob.sdk'

The MobSDK version code is as follows, remember to modify it to your own appKey和appSecret, otherwise it may not be enough:

MobSDK {
    
    
    appKey "31d18b327d099"     //修改为你自己的appKey
    appSecret "5e6a2e16f58f9c1e374acf77abb70b70"    //修改为你自己的appSecret
    SMSSDK {
    
    }
}

5. Write the MainActivity file.

The main code is as follows:
Activity main code:

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.mob.MobSDK;

import org.json.JSONException;
import org.json.JSONObject;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class MainActivity extends AppCompatActivity {
    
    
    EventHandler handler;
    EditText editText;
    EditText editText1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MobSDK.init(this, "31d18b327d099","5e6a2e16f58f9c1e374acf77abb70b70");       //记得此处修改为你的AppKey和AppSecret
        editText=findViewById(R.id.yanzhengma);
        editText1=findViewById(R.id.phone);
        handler = new EventHandler(){
    
    
            @Override
            public void afterEvent(int event, int result, Object data) {
    
    
                if (result == SMSSDK.RESULT_COMPLETE){
    
    
                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
    
    
                        //提交验证码成功
                        runOnUiThread(new Runnable() {
    
    
                            @Override
                            public void run() {
    
    
                                Toast.makeText(MainActivity.this,"验证成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                                startActivity(intent);
                            }
                        });

                    }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
    
    
                        //获取验证码成功
                        runOnUiThread(new Runnable() {
    
    
                            @Override
                            public void run() {
    
    
                                Toast.makeText(MainActivity.this,"验证码已发送", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
    
    
                    }
                }else{
    
    
                    ((Throwable)data).printStackTrace();
                    Throwable throwable = (Throwable) data;
                    try {
    
    
                        JSONObject obj = new JSONObject(throwable.getMessage());
                        final String des = obj.optString("detail");
                        if (!TextUtils.isEmpty(des)){
    
    
                            runOnUiThread(new Runnable() {
    
    
                                @Override
                                public void run() {
    
    
                                    Toast.makeText(MainActivity.this,"提交错误信息", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    } catch (JSONException e) {
    
    
                        e.printStackTrace();
                    }

                }
            }
        };

        SMSSDK.registerEventHandler(handler);
    }
    
    //提交验证
    public void tijiao(View view) {
    
    
        String number = editText.getText().toString();
        String phone=editText1.getText().toString();
        SMSSDK.submitVerificationCode("86",phone,number);
    }
    
    //点击发送验证码
    public void play(View view) {
    
    
        //获取验证码
        String phone=editText1.getText().toString();
        SMSSDK.getVerificationCode("86",phone);
    }
}

The main code of xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <EditText
        android:id="@+id/phone"
        android:inputType="numberPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入手机号码"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送短信验证码"
        android:onClick="play"
        />
    <EditText
        android:id="@+id/yanzhengma"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入手机验证码"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="tijiao"
        android:text="提交"
        />
</LinearLayout>

If you feel that the code is not enough, I will attach all the code project addresses:

The csdn address is set to 0 points and can be downloaded freely: https://download.csdn.net/download/qq_45137584/13737566

github address:
some time to pass

Step 4: Look at the effect of the project.

Because in order to protect privacy, I 手机号输入框set it as a password. But it will not affect our viewing effect. This test can be done here 手机模拟器上运行, just enter the mobile phone number 不一定要输入是你注册mob的那个手机号. The effect is as follows:
Insert picture description here
because I entered the phone number, all the sent verification codes are not sent to the mobile phone simulator, and the mobile phone simulator does not have a mobile phone number, so a screenshot of the local phone receiving verification code is attached below to verify the authenticity .
Insert picture description here
Congratulations, the desired effect has been completed. If you encounter any problems, you can raise them, because after all, I can't consider this situation alone.
功能虽然简单,但是步骤并不少,只要细心肯定是可以实现的。
If you want to know how to implement the mobile push function, you can read this blog. Android teaches you how to easily realize the push function of mobile phones, and teach you Mob+MobPush step by step to realize the push function?

Guess you like

Origin blog.csdn.net/qq_45137584/article/details/111414308