AndroidStudio使用Bmob提供的云端服务实现简单注册,登录。
来源:互联网 发布:数组转list 编辑:程序博客网 时间:2024/06/13 22:14
注册,登录用的最多的当然还是手机号和第三方平台提供的,今天就来实现一下使用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提供的文档来进一步完善此功能。
阅读全文
0 0
- AndroidStudio使用Bmob提供的云端服务实现简单注册,登录。
- 使用Bmob云端服务器模拟简单的注册登录
- 利用bmob实现简单的注册和登录
- android studio使用Bmob来实现登录注册的功能
- android studio使用Bmob来实现登录注册的功能
- Bmob后端云的使用(1)——入门指导及实现登录注册功能
- 智能厨房重构-使用Bmob后端云实现用户注册登录的功能。
- 智能厨房重构-使用Bmob后端云实现用户注册登录的功能。
- Bmob的简单使用
- Android实践——使用Bmob实现登录、注册等功能
- 使用Bmob为你的移动应用提供后端数据服务
- Android后台云端服务Bmob导入大坑
- Bmob 移动后端云服务器平台实现登录注册
- 一种为使用Log4j的分布式应用提供云端日志服务的方法
- 利用Bmob公司的云服务实现获取短信验证码登录操作
- 利用Bmob公司的云服务实现获取短信验证码登录操作
- 实现简单登录注册
- 登录、注册中校验码的简单实现
- WPF和WindowsForm下的按下Enter跳转下一个控件通用方法
- cassandra 数据库
- 关于cd命令的一些注意
- Thread 初学(二)——线程同步
- window对象
- AndroidStudio使用Bmob提供的云端服务实现简单注册,登录。
- adb批量操作bash脚本
- vm8.0安装mac提示中断导致虚拟cpu进入终止状态……vm重启的解决办法
- QT 窗口操作
- Oracle中针对一张表建立触发器,并且触发器也处理同一张表的数据(ora-04091 错误)
- c++ 指针
- 关于系统编码的那点事
- OpenStack QoS
- 在Window环境下多线程与CPU资源分配原则