• Android + Bmob 实现用户注册登录


    用户登录思路

    • 如果已经登录APP, 跳转到主页面
    • 如果没有登录APP,跳转到登录界面
      • 没有账号 —— 注册
      • 已有账号 —— 登录

    具体实现

    1. 在Bmob里新建一个应用

    1.1 登录/注册Bmob后,点击创建应用按钮

    在这里插入图片描述

    1.2 起名 —— 选择开发免费版 —— 创建应用,就ok啦

    在这里插入图片描述

    1.3 查看Application ID

    点开创建的应用,在设置里面可以看到非常重要的 Application ID(后面会用到)
    在这里插入图片描述

    2. Bmob SDK导入

    2.1添加依赖

    参考官方文档:http://doc.bmob.cn/data/android/index.html#sdk_1

    appbuild.gradle文件中添加依赖文件

    dependencies {
        implementation 'io.github.bmob:android-sdk:3.8.13'
        implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
        implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
        implementation 'com.squareup.okhttp3:okhttp:4.8.1'
        implementation 'com.squareup.okio:okio:2.2.2'
        implementation 'com.google.code.gson:gson:2.8.5'
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    同步后,可以看到
    在这里插入图片描述

    2.2 配置AndroidManifest.xml

    参考官方文档:http://doc.bmob.cn/data/android/index.html#androidmanifestxml

    AndroidManifest.xml文件中添加相应的权限:

    <!--允许联网 --> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <!--获取GSM(2g)、WCDMA(联通3g)等网络状态的信息  --> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <!--获取wifi网络状态的信息 --> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <!--保持CPU 运转,屏幕和键盘灯有可能是关闭的,用于文件上传和下载 -->
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <!--获取sd卡写的权限,用于文件上传和下载-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!--允许读取手机状态 用于创建BmobInstallation--> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    2.3 配置ContentProvider

    AndroidManifest.xml文件的application里添加

    <application>
    ···
    <provider
        android:name="cn.bmob.v3.util.BmobContentProvider"
        android:authorities="你的应用包名.BmobContentProvider">
    </provider>
    ···
    </application>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    2.4 初始化BmobSDK

    哪里需要就在哪里写,我是写在SplashActivity里面的(Splash是我的app点开的页面,我需要在这里判断是否需要登录,以及跳转操作)

    import androidx.appcompat.app.AppCompatActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import java.util.Timer;
    import java.util.TimerTask;
    import cn.bmob.v3.Bmob;
    import cn.bmob.v3.BmobUser;
    import cn.ziwei.zyapp.Bean.User;
    
    public class SplashActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_splash);
    
            // 延时操作,延时2秒
            Timer timer = new Timer();
            timer.schedule(timetask, 2000);
    
            // 初始化BmobSDK
            // 第二个参数appkey: 是第一步创建的应用里面的Application ID
            Bmob.initialize(this, "xxxxxxxxxx");
        }
    
        TimerTask timetask = new TimerTask() {
            @Override
            public void run() {
                // 判断用户是否登录
                // 	   已登录,跳转到主界面;未登陆,调转到登录界面
                BmobUser bmobUser = BmobUser.getCurrentUser(User.class);
                if (bmobUser!=null){
                    // 已登录,跳转到主界面MainActivity
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    finish();
                }else {
                    // 未登录,跳转到登录界面LoginActivity
                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                    finish();
                }
            }
        };
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    3. 实现登录操作

    3.1 LoginActivity代码
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    import cn.bmob.v3.exception.BmobException;
    import cn.bmob.v3.listener.SaveListener;
    import cn.ziwei.zyapp.Bean.User;
    
    public class LoginActivity extends AppCompatActivity {
    
        private EditText username, password;      // 登录年龄、密码
        private Button login_btn, register_btn;   // 登录按钮、注册按钮
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            username = findViewById(R.id.username);
            password = findViewById(R.id.password);
            login_btn = findViewById(R.id.login);
            register_btn = findViewById(R.id.register);
    
            // 登录按钮,设置点击事件
            login_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // 登录
                    User user = new User(); // 自己写的User类,继承BmobUser,只有一个nickname属性
                    // 设置登录的账号
                    user.setUsername(username.getText().toString().trim());
                    // 设置登录的密码
                    user.setPassword(password.getText().toString().trim());
                    user.login(new SaveListener<User>() {
                        @Override
                        public void done(User bmobUser, BmobException e) {
                            if (e == null) {
                                // 登录成功,跳转到主界面
                                Toast.makeText(LoginActivity.this, "登录成功!", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                finish();
                            } else {
                                Toast.makeText(LoginActivity.this, "用户名/密码错误,登录失败!", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            });
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    3.2 activity_login.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_margin="20dp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <LinearLayout
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="50dp"
                android:gravity="center"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="账号:"
                    android:textColor="#000"
                    android:textSize="24sp"
                    android:textStyle="bold" />
    
                <EditText
                    android:id="@+id/username"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="请输入账号" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:orientation="horizontal"
                android:layout_width="300dp"
                android:layout_height="wrap_content">
    
                <TextView
                    android:textColor="#000"
                    android:textStyle="bold"
                    android:textSize="24sp"
                    android:text="密码:"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
    
                <EditText
                    android:id="@+id/password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="请输入密码"/>
            </LinearLayout>
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
            <Button
                android:id="@+id/login"
                android:layout_margin="20dp"
                android:textSize="24sp"
                android:textColor="#fff"
                android:background="@color/cardview_dark_background"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="登录"/>
            <Button
                android:id="@+id/register"
                android:layout_margin="20dp"
                android:textSize="24sp"
                android:textColor="#fff"
                android:background="@color/cardview_dark_background"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="注册"/>
        </LinearLayout>
    </LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    4. 在Bmob应用,添加用户的数据

    打开应用,点击数据,会有一个默认的_User表

    首先添加自己需要的列:username,password(和代码里面的id保持一致)

    然后添加行,也就是数据。再进行登录操作的时候,回到这里来检测,用户名称、密码是否正确

    在这里插入图片描述

    4. 实现注册操作

    4.1 在实现登录界面已经设置了注册按钮,于是在LoginActicity添加注册按钮的点击事件
    // 注册按钮,设置点击事件
    register_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
        }
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    4.2 RegisterActivity
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    import cn.bmob.v3.exception.BmobException;
    import cn.bmob.v3.listener.SaveListener;
    import cn.ziwei.zyapp.Bean.User;
    
    public class RegisterActivity extends AppCompatActivity {
    
        private EditText username, password, nickname;   // 登录年龄、密码、昵称
        private Button register_btn;                     // 注册按钮
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_register);
            username = findViewById(R.id.username);
            password = findViewById(R.id.password);
            nickname = findViewById(R.id.nickname);
            register_btn = findViewById(R.id.register);
    
            // 注册按钮,设置点击事件
            register_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    User user = new User();
                    user.setUsername(username.getText().toString().trim());
                    user.setPassword(username.getText().toString().trim());
                    user.setNickname(nickname.getText().toString().trim());
    
                    // 判断用户名是否为空
                    if (username.getText().toString().equals("")){
                        Toast.makeText(RegisterActivity.this, "用户名为空", Toast.LENGTH_SHORT).show();
                    }
                    // 判断密码是否为空
                    if (password.getText().toString().equals("")){
                        Toast.makeText(RegisterActivity.this, "用户密码为空", Toast.LENGTH_SHORT).show();
                    }
    
                    // 注册
                    user.signUp(new SaveListener<User>() {
                        @Override
                        public void done(User user, BmobException e) {
                            if (e == null){
                                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                finish();
                                // 跳转到登录界面
                                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                            }else {
                                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            });
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    4.3 activity_register.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_margin="20dp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <LinearLayout
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="50dp"
                android:gravity="center"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="账号:"
                    android:textColor="#000"
                    android:textSize="24sp"
                    android:textStyle="bold" />
    
                <EditText
                    android:id="@+id/username"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="请输入账号" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:orientation="horizontal"
                android:layout_width="300dp"
                android:layout_height="wrap_content">
    
                <TextView
                    android:textColor="#000"
                    android:textStyle="bold"
                    android:textSize="24sp"
                    android:text="密码:"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
    
                <EditText
                    android:id="@+id/password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="请输入密码"/>
            </LinearLayout>
    
            <LinearLayout
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:orientation="horizontal"
                android:layout_width="300dp"
                android:layout_height="wrap_content">
    
                <TextView
                    android:textColor="#000"
                    android:textStyle="bold"
                    android:textSize="24sp"
                    android:text="昵称:"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
    
                <EditText
                    android:id="@+id/nickname"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="请输入昵称"/>
            </LinearLayout>
        </LinearLayout>
    
        <Button
            android:id="@+id/register"
            android:layout_margin="20dp"
            android:textSize="24sp"
            android:textColor="#fff"
            android:background="@color/cardview_dark_background"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="注册"/>
    </LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    4.4 可以查看Bmob里面的表,新数据已存在

    在这里插入图片描述

    这样一个简单的用户登录注册,就实现啦~~~
  • 相关阅读:
    请实现一个函数,输入一个整数数组和一个目标值,在数组中找到两个数使得它们的和等于目标值。
    java:CompletableFuture的简单例子
    操作系统MIT6.S081:P6->Page faults
    新手答疑解惑,Ajax 到底是啥,新手必看
    懒人方案-半天搞定一个分布式后台管理系统
    终于有多位大神联手把计算机基础知识与操作系统讲清楚了
    c++自定义sort()函数的排序方法
    layUI项目之(待开会议&历史会议&所有会议)
    奇虎360Java笔试题
    Flutter CustomPainter实现手写签名并保存为图片且去掉多余空白
  • 原文地址:https://blog.csdn.net/lameraaa/article/details/125626762