AndroidStudio使用Bmob提供的云端服务实现简单注册,登录。

注册,登录用的最多的当然还是手机号和第三方平台提供的,今天就来实现一下使用Bmob提供的云端服务实现一个简单的注册,登录功能。
要想使用Bmob云端服务需要到官网去注册获得开发者资格,注册地址:https://www.bmob.cn/register
这里写图片描述
完成注册后登录账号到网站后台创建应用,选择相应的应用类型即可。
这里写图片描述
创建完成后点击你的应用
这里写图片描述
跳转到相应的页面后点击设置你就会看到自己获得的APPID及秘钥
这里写图片描述
下面开始我们的集成之路吧
新建一个项目名为BmobLoginDemo 然后在Project的build.gradle中添加Bmob仓库地址

allprojects {
    repositories {
        jcenter()
        //Bmob的maven仓库地址,必须填写
        maven { url "https://raw.github.com/bmob/bmob-android-sdk/master" }
    }
}

然后在App的build.gradle中添加依赖库

compile 'cn.bmob.android:bmob-sdk:3.4.7-aar'

打开Androidmanifest在里面添加访问网络权限

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

下面就是代码部分了布局文件activity_main

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.shiran.bmoblogindemo.MainActivity">


   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_margin="5dp"
       android:orientation="vertical">

    <EditText
        android:id="@+id/et_user_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户名"/>

    <EditText
        android:id="@+id/et_user_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="密码"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击注册"
        android:onClick="btnShow"
        android:textSize="16sp"/>

    <Button
        android:id="@+id/btn_go_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="去登录"
        android:textSize="16sp"/>

    </LinearLayout>
</LinearLayout>

MainActivity中的代码

package com.shiran.bmoblogindemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static final String APP_ID = ""; //把你在Bmob官网获取的APPID放到这里
    private EditText mEtUserName = null;
    private EditText mEtPassWord = null;
    private Button mBtnGoLogin = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bmob.initialize(this,APP_ID); //初始化BmobSDK
        mEtUserName = (EditText) findViewById(R.id.et_user_name);
        mEtPassWord = (EditText) findViewById(R.id.et_user_pwd);
        mBtnGoLogin = (Button) findViewById(R.id.btn_go_login);
        mBtnGoLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
    }

    public void btnShow(View v) {
        final String username = mEtUserName.getText().toString();
        final String password = mEtPassWord.getText().toString();

        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            Toast.makeText(MainActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (mEtUserName.length() < 4) {
            Toast.makeText(this, "用户名不能少于4位", Toast.LENGTH_SHORT).show();
            return;
        }

        if (mEtPassWord.length() < 6) {
            Toast.makeText(this, "密码不能低于6位", Toast.LENGTH_SHORT).show();
            return;
        }

        /**
         * Bmob注册
         */
        final BmobUser user = new BmobUser();
        user.setUsername(username);
        user.setPassword(password);
        user.signUp(MainActivity.this, new SaveListener() { //回调2个方法,成功,失败
            @Override
            public void onSuccess() {
                Toast.makeText(MainActivity.this, "注册成功,去登录", Toast.LENGTH_SHORT).show();
                Log.e(TAG, "onSuccess: "+ "注册成功");
            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(MainActivity.this, "注册失败,用户名已存在或网络问题", Toast.LENGTH_SHORT).show();
                Log.e(TAG, "onFailure: "+s.toString());

            }
        });
    }
}

到这里注册就已经完成了,接下来就是LoginActivity中的代码
布局文件activity_login

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.shiran.bmoblogindemo.LoginActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="vertical">

     <EditText
         android:id="@+id/et_login_name"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="请输入您的账号"/>

     <EditText
         android:id="@+id/et_login_pwd"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:inputType="textPassword"
         android:hint="请输入您的密码"/>

     <Button
         android:id="@+id/btn_login"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="点击登录"
         android:onClick="btnShowLogin"
         android:textSize="16sp"/>

 </LinearLayout>
</LinearLayout>

LoginActivity.java中的代码

package com.shiran.bmoblogindemo;

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

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;

public class LoginActivity extends AppCompatActivity {
    private EditText mEtUserName = null;
    private EditText mEtPassWord = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mEtUserName = (EditText) findViewById(R.id.et_login_name);
        mEtPassWord = (EditText) findViewById(R.id.et_login_pwd);

    }

    public void btnShowLogin(View v) {
        final String username = mEtUserName.getText().toString();
        final String password = mEtPassWord.getText().toString();

        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            Toast.makeText(LoginActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        final BmobUser user = new BmobUser();
        user.setUsername(username);
        user.setPassword(password);
        user.login(LoginActivity.this, new SaveListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(LoginActivity.this, LoginSuccessActivity.class);
                startActivity(intent);

            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(LoginActivity.this, "登录失败,请检查用户名及密码", Toast.LENGTH_SHORT).show();

            }
        });
    }
}

登录中的代码基本和注册时的代码一样只是把user.signUp改成了user.login
最后把登录成功后的代码也一块贴上来吧
布局文件activity_login_success

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.shiran.bmoblogindemo.LoginSuccessActivity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Login Success"
        android:textSize="18sp"/>

</RelativeLayout>

LoginSuccessActivity.Java代码

package com.shiran.bmoblogindemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class LoginSuccessActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_success);
    }
}

来看一下效果图
这里写图片描述这里写图片描述
打开Bmob的控制台找到自己刚才创建的应用,你会看到云数据库中有一个_User表刚才我们注册的用户名和密码已经保存到了这个表中
这里写图片描述
已经完成了一个简单的注册登录,大家有兴趣的话可以根据Bmob提供的文档来进一步完善此功能。

猜你喜欢

转载自blog.csdn.net/sandyran/article/details/74984475