安卓SDK——Bmob云服务(登录注册)

接上

安卓SDK——Bmob云服务(增删改查)

https://blog.csdn.net/nishigesb123/article/details/89854563


基于Bmob云服务实现用户注册和登录

准备一个实体类User,后面会用到,但是Bmob本身就已经有User对象,所以继承BmobUser

指的就是我们进入控制台默认看到的这个,由于大部分字段都已经提供了,我们只需要补写一些在默认表中没有提供的。

package com.example.bombtest;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;

public class User extends BmobUser {
    private BmobFile icon;

    public BmobFile getIcon() {
        return icon;
    }

    public void setIcon(BmobFile icon) {
        this.icon = icon;
    }
}

新建一个StartActivty,并作为启动(登录)界面

布局的话就比较随意了,偷懒直接拖拽了← ←,所以代码...emm

首先是布局,需要两个按钮及两个输入框(EditText)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".StartActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:ems="10"
        android:hint="请输入用户名"
        android:inputType="textPersonName"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="0dp" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp"
        android:ems="10"
        android:hint="请输入密码"
        android:inputType="textPassword"
        app:layout_constraintTop_toBottomOf="@+id/editText"
        tools:layout_editor_absoluteX="0dp" />

    <Button
        android:id="@+id/button5"
        android:layout_width="100dp"
        android:layout_height="51dp"
        android:layout_marginTop="28dp"
        android:layout_marginEnd="100dp"
        android:onClick="login"
        android:text="登录"
        app:layout_constraintEnd_toStartOf="@+id/button6"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />

    <Button
        android:id="@+id/button6"
        android:layout_width="100dp"
        android:layout_height="51dp"
        android:layout_marginTop="28dp"
        android:layout_marginEnd="44dp"
        android:onClick="register"
        android:text="注册"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />

</android.support.constraint.ConstraintLayout>

主要是书写登录按钮点击事件

package com.example.bombtest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class StartActivity extends AppCompatActivity {

    private EditText username,password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        username = findViewById(R.id.editText);
        password = findViewById(R.id.editText2);
        Bmob.initialize(this, "0bc8b0a9e9aea4bb31f4be7fe9e9ec38");
    }
    public void login(View view){
        String name = username.getText().toString();
        String pass = password.getText().toString();

        User user = new User();
        user.setUsername(name);
        user.setPassword(pass);
        user.login(new SaveListener<User>() {
            @Override
            public void done(User bmobUser, BmobException e) {
                if (e == null) {
                    User user = BmobUser.getCurrentUser(User.class);
                    Toast.makeText(StartActivity.this,  "登录成功:" + user.getUsername(),Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(StartActivity.this, "登录失败:" + e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    //跳转到注册界面
    public void register(View view){
        Intent intent = new Intent(this, RegActivity.class);
        startActivity(intent);
    }
}

再准备一共RegActivty作为注册界面,布局需要一个按钮和四个输入框,方便测试,头像就默认为外部存储的某张图片了....

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RegActivity">

    <EditText
        android:id="@+id/editText6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="45dp"
        android:ems="10"
        android:hint="请输入用户名"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText7"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="33dp"
        android:ems="10"
        android:hint="请输入密码"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText6" />

    <EditText
        android:id="@+id/editText8"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="33dp"
        android:ems="10"
        android:hint="请输入邮箱"
        android:inputType="textEmailAddress"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText7" />

    <EditText
        android:id="@+id/editText9"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:hint="头像"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText8" />

    <Button
        android:id="@+id/button7"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:text="注册"
        android:onClick="register"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText9" />
</android.support.constraint.ConstraintLayout>

涉及到了文件读写主要是图标的上传所以需要权限(主要是读)

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

代码部分主要是register按钮的点击事件

package com.example.bombtest;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;

import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadFileListener;

public class RegActivity extends AppCompatActivity {

    private EditText username, password, e_email, e_icon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        username = findViewById(R.id.editText6);
        password = findViewById(R.id.editText7);
        e_email = findViewById(R.id.editText8);
        e_icon = findViewById(R.id.editText9);

    }

    public void register(View view) {
        final String name = username.getText().toString();
        final String pass = password.getText().toString();
        final String email = e_email.getText().toString();
        final String icon = e_icon.getText().toString();


        //获取图片文件路径
        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/test.jpg";
        //图片上传
        final BmobFile bmobFile = new BmobFile(new File(path));
        bmobFile.uploadblock(new UploadFileListener() {

            @Override
            public void done(BmobException e) {
                if (e == null) {
                    //bmobFile.getFileUrl()--返回的上传文件的完整地址
                    Toast.makeText(RegActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
                    User user = new User();
                    user.setUsername(name);
                    user.setPassword(pass);
                    user.setEmail(email);
                    user.setIcon(bmobFile);
                    //进行用户注册
                    user.signUp(new SaveListener<User>() {
                        @Override
                        public void done(User user, BmobException e) {
                            if (e == null) {
                                Toast.makeText(RegActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(RegActivity.this, "注册失败"+e.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                } else {
                    Toast.makeText(RegActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onProgress(Integer value) {
                // 返回的上传进度(百分比)
            }
        });
    }
}

注册测试

注册效果如下

 

还有一个注册成功的提示,手慢没截到,只能再注册一个号了...

查看控制台,确实已经出现了对应的数据,并且上传的头像(test.jsp)也已经上传成功了

如果设置了邮箱服务还可以收到一封提示激活的信件

回到控制台,会发现账号的emailverified变为了true

登录测试

输入刚才注册的用户名和密码

如果输入错误的密码或账号

读者可以自行将toast语句换成自己想要执行的效果,如登录成功后跳转到登录后的Activty等...下面简单的丰富一下代码


跳转

现在登录后会跳转到MainActivity,左下角会显示当前登录的用户信息

修改后的MainActivity

package com.example.bombtest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        imageView = findViewById(R.id.imageView);
        //初始化BmobSDK
        //Bmob.initialize(this, "0bc8b0a9e9aea4bb31f4be7fe9e9ec38");
        Intent intent = getIntent();
        User user = (User) intent.getSerializableExtra("user");
        textView.setText("欢迎您:"+user.getUsername());

    }

    //添加数据
    public void add(View view) {
        Person p = new Person();
        p.setName("添加数据的姓名");
        p.setAddress("添加数据的地址");
        p.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (e == null) {
                    Toast.makeText(MainActivity.this, "添加数据成功,返回objectId为" + s, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "创建数据失败,返回Exception为:" + e, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //删除数据
    public void delete(View v) {
        Person p = new Person();
        p.setObjectId("eee8101a84");
        p.delete(new UpdateListener() {

            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Toast.makeText(MainActivity.this, "删除数据成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "删除数据失败,返回Exception为:" + e, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void update(View v) {
        Person p = new Person();
        p.setAddress("被修改的地址");
        p.update("eee8101a84", new UpdateListener() {

            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Toast.makeText(MainActivity.this, "更新数据成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "更新数据失败返回Exception为:" + e, Toast.LENGTH_SHORT).show();
                }
            }

        });
    }

    public void query(View v) {
        final BmobQuery<Person> bmobQuery = new BmobQuery<Person>();
        bmobQuery.getObject("eee8101a84", new QueryListener<Person>() {
            @Override
            public void done(Person object, BmobException e) {
                if (e == null) {
                    Toast.makeText(MainActivity.this, "查询数据成功,查询的数据内容为"+object.getName()+","+object.getAddress(), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "查询数据失败,返回Exception为:" + e, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


}

修改后的StartActivity

package com.example.bombtest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class StartActivity extends AppCompatActivity {

    private EditText username,password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        username = findViewById(R.id.editText);
        password = findViewById(R.id.editText2);
        Bmob.initialize(this, "0bc8b0a9e9aea4bb31f4be7fe9e9ec38");
    }
    public void login(View view){
        String name = username.getText().toString();
        String pass = password.getText().toString();

        User user = new User();
        user.setUsername(name);
        user.setPassword(pass);
        user.login(new SaveListener<User>() {
            @Override
            public void done(User bmobUser, BmobException e) {
                if (e == null) {
                    User user = BmobUser.getCurrentUser(User.class);
                    if(user.getEmailVerified()){
                        Toast.makeText(StartActivity.this,  "登录成功:" + user.getUsername(),Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(StartActivity.this, MainActivity.class);
                        intent.putExtra("user", user);
                        startActivity(intent);
                    }else{
                        Toast.makeText(StartActivity.this,  "用户未激活:" + user.getUsername(),Toast.LENGTH_LONG).show();
                    }

                } else {
                    Toast.makeText(StartActivity.this, "登录失败:" + e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    //跳转到注册界面
    public void register(View view){
        Intent intent = new Intent(this, RegActivity.class);
        startActivity(intent);
    }
}

 

参与评论 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页

打赏作者

旧日灯塔

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值