目录
前言
概览
Bmob后端云介绍
Bmob后端云与Android Studio配置
一、Bmob后端云
二、Android Studio配置
工具类
一、User类
二、Suit类
三、Code类
实现类
一、登录代码
二、注册代码
三、找回密码代码
四、想法反馈代码
前言
本项目尚在开发阶段,主要针对的是安卓用户,通过查阅开发文档以及借鉴其他博主,由于是个人开发,可能会在逻辑会有所缺漏,欢迎各位安卓使用者与开发者进行内测并提交反馈意见给我,进行探讨顺便带带我;
本项目开发主题尚未决定,故本期向各位征集意见,可以通过安装该app进行注册登录,并写下您的想法提交反馈给我,后续将对收集的idea进行整合并决定方向,进行下一步的开发。
ps:保证无病毒与无信息泄露!!!欢迎大家参与
源代码:
链接:https://pan.baidu.com/s/10ECb54ne_9bubuCLWLB-Kw?pwd=v2sh
提取码:v2sh
安卓手机软件安装包:
链接:https://pan.baidu.com/s/1a2H5CKxKvtAjvnyOHvybUQ?pwd=np3w
提取码:np3w
概览
本期主要用到两个工具,Android Studio和Bmob后端云(后续介绍),在安卓客户端注册登陆并提交想法,最终在Bmob后端云中实现数据可视化,主要实现效果如下图所示
- Android Studio
- Bmob后端云
Android Studio在此不做介绍了,以下是对Bmob后端云的介绍
Bmob后端云介绍
本次采用Bmob后端云(Bmob后端云 (bmobapp.com))主要是其集成了数据库与服务器,对于很多Android/ios/wp个人移动开发者来说,开发一个具有网络功能的应用不是一件容易的事,不仅需要购买/租赁服务器,还必须掌握一门诸如Java/.net/php这类的服务器开发语言,每开发一款移动应用程序,就必须开发维护对应的服务器程序,鉴于此我目前水平有限,故先采用Bmob后端云进行开发,给应用软件快速添加一个安全灵活的后台管理系统,方便浏览终端保存的各种信息。
Bmob后端云与Android Studio配置
一、Bmob后端云
- 官网注册账号 (Bmob后端云 (bmobapp.com))
- 创建应用
- 创建表单,默认有一个_User表单(不用管),要新建一个User表单(名称要与后边安卓代码相同),数据将会在这个表单呈现
- 获取key秘钥,后边需要用
二、Android Studio配置
- AndroidManifest.xml:
1、添加网络权限
在上图位置添加下方代码以添加网络权限
<!--允许联网 -->
<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" />
2、连接Bmob
在上图位置添加下方代码,并将蓝色方框中的代码替换成红色箭头所指的自己代码的包名
<provider
android:name="cn.bmob.v3.util.BmobContentProvider"
android:authorities="包名.BmobContentProvider">
</provider>
- 在Activity中对Bmob进行初始化,在OnCreat方法中添加下方代码并将你的应用ID替换成上方Bmob配置时获取的key密钥
Bmob.initialize(this,"你的应用ID");
- 在build.gradle(Module:app)中添加包
implementation 'io.github.bmob:android-sdk:3.9.4'
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'
至此,配置已全部完成,接下来是进行界面设计与逻辑代码编写环节
工具类
一、User类
这个工具类的名称与前边在Bmob创建的表单名称相同,这个类继承于BmobObject,其属性相当于在表中的数据。
package com.example.application.tool;
import java.io.Serializable;
import cn.bmob.v3.BmobObject;
public class User extends BmobObject implements Serializable {
private String Acctount;//手机账户
private String Password;//密码
private String Context;//反馈内容
public String getAcctount() {
return Acctount;
}
public void setAcctount(String acctount) {
Acctount = acctount;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
public String getContext() {
return Context;
}
public void setContext(String context) {
Context = context;
}
}
二、Suit类
这个类主要是号码检测(是否符合格式)与密码检测(至少包含3个字母)
package com.example.application.tool;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Suit {
//手机号码检测
public static boolean PhoneSuit(String phone){
String ChineseMainland = "^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])" + "|(18[0-9])|(19[8,9]))\\d{8}$";
String HongKong = "^(5|6|8|9)\\d{7}$";
Matcher C = Pattern.compile(ChineseMainland).matcher(phone);
Matcher H = Pattern.compile(HongKong).matcher(phone);
return C.matches() || H.matches();
}
//密码检测(最少3个字母)
public static boolean PasswordSuit(String password){
char[] s = password.toCharArray();
int count = 0 ;
//检测所有的密码中有多少个字母
for (int i = 0;i < s.length;i++){
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')){
count++;
}
}
if (count >= 3){
return true;
}else {
return false;
}
}
}
三、Code类
这个类主要是生成验证码图片,由于目前短信验证在资源上有数量限制,故采用验证码图片进行验证,在实现类中通过创建类对象调用类方法实现对验证码图片的生成。
package com.example.application.tool;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import java.util.Random;
public class Code {
//随机数数组
private static final char[] CHARS = {
'2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm',
'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
private static Code bmpCode;
public static Code getInstance() {
if(bmpCode == null)
bmpCode = new Code();
return bmpCode;
}
//default settings
//验证码默认随机数的个数
private static final int DEFAULT_CODE_LENGTH = 4;
//默认字体大小
private static final int DEFAULT_FONT_SIZE = 25;
//默认线条的条数
private static final int DEFAULT_LINE_NUMBER = 5;
//padding值
private static final int BASE_PADDING_LEFT = 10, RANGE_PADDING_LEFT = 15, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 20;
//验证码的默认宽高
private static final int DEFAULT_WIDTH = 100, DEFAULT_HEIGHT = 40;
//settings decided by the layout xml
//canvas width and height
private int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT;
//random word space and pading_top
private int base_padding_left = BASE_PADDING_LEFT, range_padding_left = RANGE_PADDING_LEFT,
base_padding_top = BASE_PADDING_TOP, range_padding_top = RANGE_PADDING_TOP;
//number of chars, lines; font size
private int codeLength = DEFAULT_CODE_LENGTH, line_number = DEFAULT_LINE_NUMBER, font_size = DEFAULT_FONT_SIZE;
//variables
private String code;
private int padding_left, padding_top;
private Random random = new Random();
//验证码图片
public Bitmap createBitmap() {
padding_left = 0;
Bitmap bp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bp);
code = createCode();
c.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(font_size);
//画验证码
for (int i = 0; i < code.length(); i++) {
randomTextStyle(paint);
randomPadding();
c.drawText(code.charAt(i) + "", padding_left, padding_top, paint);
}
//画线条
for (int i = 0; i < line_number; i++) {
drawLine(c, paint);
}
//c.save(Canvas.ALL_SAVE_FLAG);//保存
c.save();
c.restore();
return bp;
}
public String getCode() {
return code;
}
//生成验证码
private String createCode() {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < codeLength; i++) {
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
return buffer.toString();
}
//画干扰线
private void drawLine(Canvas canvas, Paint paint) {
int color = randomColor();
int startX = random.nextInt(width);
int startY = random.nextInt(height);
int stopX = random.nextInt(width);
int stopY = random.nextInt(height);
paint.setStrokeWidth(1);
paint.setColor(color);
canvas.drawLine(startX, startY, stopX, stopY, paint);
}
//生成随机颜色
private int randomColor() {
return randomColor(1);
}
private int randomColor(int rate) {
int red = random.nextInt(256) / rate;
int green = random.nextInt(256) / rate;
int blue = random.nextInt(256) / rate;
return Color.rgb(red, green, blue);
}
//随机生成文字样式,颜色,粗细,倾斜度
private void randomTextStyle(Paint paint) {
int color = randomColor();
paint.setColor(color);
paint.setFakeBoldText(random.nextBoolean()); //true为粗体,false为非粗体
float skewX = random.nextInt(11) / 10;
skewX = random.nextBoolean() ? skewX : -skewX;
paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜
}
//随机生成padding值
private void randomPadding() {
padding_left += base_padding_left + random.nextInt(range_padding_left);
padding_top = base_padding_top + random.nextInt(range_padding_top);
}
}
实现类
一、登录代码
- 布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="@mipmap/fm"
android:orientation="vertical"
tools:context=".activity.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="serif"
android:gravity="center"
android:padding="80dp"
android:text="Login"
android:textColor="@color/white"
android:textSize="50dp"
>
</TextView>
</LinearLayout>
<LinearLayout
android:paddingHorizontal="15dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="@mipmap/phone">
</TextView>
<EditText
android:id="@+id/Login_Phone"
android:hint="请输入手机号码"
android:textSize="25dp"
android:theme="@style/MyEditText"
android:fontFamily="sans-serif-condensed-medium"
android:textColorHint="#B0BEC5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</EditText>
</LinearLayout>
<LinearLayout
android:paddingHorizontal="15dp"
android:paddingTop="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="@mipmap/password">
</TextView>
<EditText
android:id="@+id/Login_Password"
android:hint="请输入密码"
android:textSize="25dp"
android:theme="@style/MyEditText"
android:fontFamily="sans-serif-condensed-medium"
android:textColorHint="#B0BEC5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</EditText>
</LinearLayout>
<TextView
android:id="@+id/Login"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:background="@mipmap/jiantou"
android:layout_width="80dp"
android:layout_height="80dp">
</TextView>
<LinearLayout
android:paddingHorizontal="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/Login_Lose"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="忘记密码?"
android:textColor="#ECEFF1"
android:textSize="25dp"
>
</TextView>
<TextView
android:id="@+id/Login_Resgister"
android:layout_marginLeft="140dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="注册账号"
android:textColor="#ECEFF1"
android:textSize="25dp"
>
</TextView>
</LinearLayout>
</LinearLayout>
- 实现代码
package com.example.application.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.application.R;
import com.example.application.tool.User;
import java.util.List;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText login_phone;//输入收集号码
private EditText login_password;//输入密码
private TextView login;//登陆按钮
private TextView login_lose;//忘记密码
private TextView login_resgister;//注册账号
private String phone;
private String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bmob.initialize(this,"ebb1d24c01600c55b1e8be7f2625d7a7");
initView();
}
//初始化控件
private void initView() {
login_phone = findViewById(R.id.Login_Phone);
login_password = findViewById(R.id.Login_Password);
login = findViewById(R.id.Login);
login_lose = findViewById(R.id.Login_Lose);
login_resgister = findViewById(R.id.Login_Resgister);
//启动监听事件
login.setOnClickListener(this);
login_lose.setOnClickListener(this);
login_resgister.setOnClickListener(this);
}
//点击事件
@Override
public void onClick(View view) {
//添加意图,切换画面
Intent intent = new Intent();
//选择监听按钮触发机制
switch (view.getId()){
case R.id.Login:
Getdata();
//没有输入号码、密码判断为空则弹窗提示
if (TextUtils.isEmpty(phone)){
Toast.makeText(this,"请输入手机号码",Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(password)) {
Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
} else {
BmobQuery<User> bmobQuery = new BmobQuery<>();
bmobQuery.findObjects(new FindListener<User>() {
@Override
public void done(List<User> object, BmobException e) {
//如果没有异常的话进行查询列表账号
if(e == null){
int count = 0;//计数
//遍历
for (User user : object) {
//查询到与客户端输入的账号一致
if (user.getAcctount().equals(phone)){
//查询到与客户端输入的密码一致
if (user.getPassword().equals(password)){
intent.setClass(getApplicationContext(),First_interface.class);
//传递对象,方便主界面获取到用户信息
intent.putExtra("User",user);
startActivity(intent);
Toast.makeText(getApplicationContext(),"登陆成功",Toast.LENGTH_SHORT).show();
break;
}else {
Toast.makeText(getApplicationContext(),"密码错误",Toast.LENGTH_SHORT).show();
}
}
count++;
}
//遍历没有查询到,此时count++到object的数量
if (count >= object.size()){
Toast.makeText(getApplicationContext(),"账号不存在",Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(),"账号不存在",Toast.LENGTH_SHORT).show();
}
}
});
}
break;
case R.id.Login_Lose:
intent.setClass(getApplicationContext(),Findpassword.class);
startActivity(intent);
break;
case R.id.Login_Resgister:
intent.setClass(getApplicationContext(),Resgister.class);
startActivity(intent);
break;
}
}
//获取输入的号码与密码
public void Getdata(){
phone = login_phone.getText().toString().trim();
password = login_password.getText().toString().trim();
}
}
二、注册代码
- 布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="@mipmap/fm"
tools:context=".activity.Resgister"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="serif"
android:gravity="center"
android:padding="80dp"
android:text="Register"
android:textColor="@color/white"
android:textSize="50dp"
>
</TextView>
</LinearLayout>
<LinearLayout
android:paddingHorizontal="15dp"
android:paddingTop="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="@mipmap/phone">
</TextView>
<EditText
android:id="@+id/Register_Phone"
android:hint="请输入手机号码"
android:textSize="25dp"
android:theme="@style/MyEditText"
android:fontFamily="sans-serif-condensed-medium"
android:textColorHint="#B0BEC5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</EditText>
</LinearLayout>
<LinearLayout
android:paddingHorizontal="15dp"
android:paddingTop="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="@mipmap/password">
</TextView>
<EditText
android:id="@+id/Register_Password"
android:hint="请输入密码"
android:textSize="25dp"
android:theme="@style/MyEditText"
android:fontFamily="sans-serif-condensed-medium"
android:textColorHint="#B0BEC5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</EditText>
</LinearLayout>
<LinearLayout
android:paddingHorizontal="15dp"
android:paddingTop="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="0.7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@mipmap/message">
</TextView>
<EditText
android:id="@+id/Register_SMS"
android:hint="请输入验证码"
android:textSize="25dp"
android:theme="@style/MyEditText"
android:fontFamily="sans-serif-condensed-medium"
android:textColorHint="#B0BEC5"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
>
</EditText>
<ImageView
android:id="@+id/Register_Paint"
android:gravity="center"
android:layout_weight="2"
android:layout_width="0dp"
android:textSize="20dp"
android:layout_height="50dp"
android:background="@color/white">
</ImageView>
</LinearLayout>
<TextView
android:id="@+id/Register"
android:layout_marginTop="10dp"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="@mipmap/register">
</TextView>
<TextView
android:id="@+id/Login_Back"
android:paddingRight="20dp"
android:gravity="end"
android:text="已有账号?去登录"
android:textColor="#ECEFF1"
android:textSize="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
- 实现代码
package com.example.application.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.application.R;
import com.example.application.tool.Code;
import com.example.application.tool.Suit;
import com.example.application.tool.User;
import java.util.List;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
public class Resgister extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = null;
private EditText register_phone;//获取注册的手机号码
private EditText register_password;//获取注册的密码
private EditText register_sms;//输入验证码
private TextView register;//注册
private TextView login_back;//返回登陆
private ImageView register_paint;//获取验证码
private String phone;//客户端输入的手机号码
private String passeword;//客户端输入的密码
private String sms;//客户端输入的验证码
private String realCode;//图形验证码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resgister);
Bmob.initialize(this,"ebb1d24c01600c55b1e8be7f2625d7a7");
initView();
}
//初始化控件
private void initView() {
//获取控件变量
register_phone = findViewById(R.id.Register_Phone);
register_password = findViewById(R.id.Register_Password);
register_sms = findViewById(R.id.Register_SMS);
register = findViewById(R.id.Register);
login_back = findViewById(R.id.Login_Back);
register_paint = findViewById(R.id.Register_Paint);
//启动监听机制
//将验证码用图片的形式显示出来
register_paint.setImageBitmap(Code.getInstance().createBitmap());
realCode = Code.getInstance().getCode().toLowerCase();
register_paint.setOnClickListener(this);
register.setOnClickListener(this);
login_back.setOnClickListener(this);
}
//获取数据
public void Getdata(){
phone = register_phone.getText().toString().trim();
passeword = register_password.getText().toString().trim();
sms = register_sms.getText().toString().toLowerCase();
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.Register_Paint:
register_paint.setImageBitmap(Code.getInstance().createBitmap());
realCode = Code.getInstance().getCode().toLowerCase();
Log.v(TAG,"realCode"+realCode);
break;
case R.id.Login_Back:
finish();
break;
case R.id.Register:
Getdata();
//输入限制条件:不能为空、号码以及密码符合格式、密码数量限制条件:6到16
if (TextUtils.isEmpty(phone)){
Toast.makeText(getApplicationContext(),"请输入手机号码",Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(passeword)) {
Toast.makeText(getApplicationContext(),"请输入密码",Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(sms)) {
Toast.makeText(getApplicationContext(),"请输入验证码",Toast.LENGTH_SHORT).show();
} else if (Suit.PhoneSuit(phone.trim()) != true) {
Toast.makeText(getApplicationContext(),"请输入正确的手机号码",Toast.LENGTH_SHORT).show();
} else if (Suit.PasswordSuit(passeword.trim()) != true) {
Toast.makeText(getApplicationContext(),"密码最少包含3个字母",Toast.LENGTH_SHORT).show();
} else if (passeword.length() < 6) {
Toast.makeText(getApplicationContext(),"密码不得少于6位数",Toast.LENGTH_SHORT).show();
} else if (passeword.length() > 16) {
Toast.makeText(getApplicationContext(),"密码不得多于16位数",Toast.LENGTH_SHORT).show();
}else {
BmobQuery<User> bmobQuery = new BmobQuery<>();
bmobQuery.findObjects(new FindListener<User>() {
@Override
public void done(List<User> object, BmobException e) {
if (e == null){
int count = 0;//判断是否查询到尾--遍历
//查询判断用户是否已经存在
for (User user : object){
if (user.getAcctount().equals(phone)){
register_phone.setText("");
Toast.makeText(getApplicationContext(),"该用户已经注册过",Toast.LENGTH_SHORT).show();
finish();
break;
}
count++;
}
//查询到尾没有
if (count == object.size()){
//判断验证码是否正确
if (sms.equals(realCode)) {
Toast.makeText(getApplicationContext(), "验证码正确", Toast.LENGTH_SHORT).show();
//将用户信息存入bmob云端
final User user = new User();
user.setAcctount(phone);
user.setPassword(passeword);
user.save(new SaveListener<String>() {
@Override
public void done(String s, BmobException e) {
if (e == null){
Toast.makeText(getApplicationContext(),"注册成功",Toast.LENGTH_SHORT).show();
finish();
}else {
Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_SHORT).show();
}
}
});
} else {
register_sms.setText("");
Toast.makeText(getApplicationContext(), "验证码错误", Toast.LENGTH_SHORT).show();
}
}
}else {
Toast.makeText(Resgister.this, "该用户不存在", Toast.LENGTH_SHORT).show();
}
}
});
}
break;
}
}
}
三、找回密码代码
- 布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:background="@mipmap/fm"
tools:context=".activity.Findpassword">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginTop="80dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="serif"
android:gravity="center"
android:padding="20dp"
android:text="FindPassword"
android:textColor="@color/white"
android:textSize="50dp"
>
</TextView>
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:paddingHorizontal="15dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="@mipmap/phone">
</TextView>
<EditText
android:id="@+id/Find_Phone"
android:hint="请输入手机号码"
android:textSize="25dp"
android:theme="@style/MyEditText"
android:fontFamily="sans-serif-condensed-medium"
android:textColorHint="#B0BEC5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</EditText>
</LinearLayout>
<LinearLayout
android:paddingHorizontal="15dp"
android:paddingTop="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="0.7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@mipmap/message">
</TextView>
<EditText
android:id="@+id/Find_SMS"
android:hint="请输入验证码"
android:textSize="25dp"
android:theme="@style/MyEditText"
android:fontFamily="sans-serif-condensed-medium"
android:textColorHint="#B0BEC5"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
>
</EditText>
<ImageView
android:id="@+id/Find_Image"
android:gravity="center"
android:layout_weight="2"
android:layout_width="0dp"
android:textSize="20dp"
android:layout_height="50dp"
android:background="@color/white">
</ImageView>
</LinearLayout>
<LinearLayout
android:paddingHorizontal="15dp"
android:paddingTop="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="@mipmap/password">
</TextView>
<EditText
android:id="@+id/Find_Password"
android:hint="请输入密码"
android:textSize="25dp"
android:theme="@style/MyEditText"
android:fontFamily="sans-serif-condensed-medium"
android:textColorHint="#B0BEC5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</EditText>
</LinearLayout>
<TextView
android:id="@+id/Findword"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:background="@mipmap/queren"
android:layout_width="80dp"
android:layout_height="80dp">
</TextView>
<TextView
android:id="@+id/Login_Go"
android:layout_marginRight="35dp"
android:text="去登陆"
android:textSize="25dp"
android:textColor="#ECEFF1"
android:gravity="end"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
- 实现代码
package com.example.application.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.application.R;
import com.example.application.tool.Code;
import com.example.application.tool.Suit;
import com.example.application.tool.User;
import java.util.List;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;
public class Findpassword extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = null;
private EditText find_phone;//手机号码
private EditText find_sms;//验证码
private EditText find_password;//重设密码
private ImageView find_image;//验证码图片
private TextView findword;//完成
private TextView login_go;//去登陆
private String realCode;
private String phone;
private String sms;
private String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_findpassword);
Bmob.initialize(this,"ebb1d24c01600c55b1e8be7f2625d7a7");
initView();
}
//初始化控件
private void initView() {
//控件变量
find_phone = findViewById(R.id.Find_Phone);
find_sms = findViewById(R.id.Find_SMS);
find_password = findViewById(R.id.Find_Password);
find_image = findViewById(R.id.Find_Image);
findword = findViewById(R.id.Findword);
login_go = findViewById(R.id.Login_Go);
//监听机制
find_image.setImageBitmap(Code.getInstance().createBitmap());
realCode = Code.getInstance().getCode().toLowerCase();
find_image.setOnClickListener(this);
findword.setOnClickListener(this);
login_go.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent intent = new Intent();
switch (view.getId()){
case R.id.Find_Image:
find_image.setImageBitmap(Code.getInstance().createBitmap());
realCode = Code.getInstance().getCode().toLowerCase();
Log.v(TAG,"realCode"+realCode);
break;
case R.id.Findword:
Getdata();
//判断不能为空
if (TextUtils.isEmpty(phone)){
Toast.makeText(getApplicationContext(),"请输入手机号码",Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(),"请输入密码",Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(sms)) {
Toast.makeText(getApplicationContext(),"请输入验证码",Toast.LENGTH_SHORT).show();
} else if (Suit.PhoneSuit(phone.trim()) != true) {
Toast.makeText(getApplicationContext(),"请输入正确的手机号码",Toast.LENGTH_SHORT).show();
} else if (Suit.PasswordSuit(password.trim()) != true) {
Toast.makeText(getApplicationContext(),"密码最少包含3个字母",Toast.LENGTH_SHORT).show();
} else if (password.length() < 6) {
Toast.makeText(getApplicationContext(),"密码不得少于6位数",Toast.LENGTH_SHORT).show();
} else if (password.length() > 16) {
Toast.makeText(getApplicationContext(), "密码不得多于16位数", Toast.LENGTH_SHORT).show();
}else {
BmobQuery<User> bmobQuery = new BmobQuery<>();
bmobQuery.findObjects(new FindListener<User>() {
@Override
public void done(List<User> object, BmobException e) {
if (e == null){
int count = 0;//判断是否查询到尾--遍历
//查询判断用户是否已经存在
for (User user : object){
if (user.getAcctount().equals(phone)){
Toast.makeText(getApplicationContext(),"该用户存在",Toast.LENGTH_SHORT).show();
//判断验证码是否正确
if (sms.equals(realCode)) {
//云端数据更新
User user1 = new User();
user1.setPassword(password);
user1.update(user.getObjectId(), new UpdateListener() {
@Override
public void done(BmobException e) {
if (e == null) {
Toast.makeText(getApplicationContext(),"密码修改成功",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(),"修改失败"+"\n"+"错误代码:"+e.getErrorCode(),Toast.LENGTH_LONG).show();
}
}
});
finish();
break;
}else {
find_sms.setText("");
Toast.makeText(getApplicationContext(), "验证码错误", Toast.LENGTH_SHORT).show();
}
}
count++;
}
//查询到尾没有
if (count >= object.size()){
Toast.makeText(getApplicationContext(),"该用户不存在",Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(getApplicationContext(),"该用户不存在",Toast.LENGTH_SHORT).show();
}
}
});
}
break;
case R.id.Login_Go:
finish();
break;
}
}
//获取输入的数据
private void Getdata() {
phone = find_phone.getText().toString().trim();
sms = find_sms.getText().toString().toLowerCase();
password = find_password.getText().toString().trim();
}
}
四、想法反馈代码
- 布局代码
文章来源:https://www.toymoban.com/news/detail-718835.html
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".activity.First_interface">
<EditText
android:id="@+id/Idea"
android:paddingTop="20dp"
android:paddingHorizontal="20dp"
android:hint="请输入您的想法"
android:gravity="left"
android:textSize="30dp"
android:background="@null"
android:textColorHint="#B0BEC5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="13">
</EditText>
<Button
android:id="@+id/submit"
android:text="提交"
android:gravity="center|bottom"
android:textSize="30dp"
android:background="#2196F3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</Button>
</LinearLayout>
- 实现代码
package com.example.application.activity; 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 com.example.application.R; import com.example.application.tool.User; import java.util.List; import cn.bmob.v3.Bmob; import cn.bmob.v3.BmobQuery; import cn.bmob.v3.exception.BmobException; import cn.bmob.v3.listener.FindListener; import cn.bmob.v3.listener.SaveListener; import cn.bmob.v3.listener.UpdateListener; public class First_interface extends AppCompatActivity implements View.OnClickListener { private EditText idea;//编辑框 private Button submit;//提交按钮 private String context;//编辑框内容 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first_interface); Bmob.initialize(this,"ebb1d24c01600c55b1e8be7f2625d7a7"); initView(); } //初始化控件 private void initView() { //定义控件 idea = findViewById(R.id.Idea); submit = findViewById(R.id.submit); //监听 submit.setOnClickListener(this); } public void Getdata(){ context = idea.getText().toString().trim(); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.submit: Getdata(); //传递对象 Intent intent = getIntent(); User user = (User) intent.getSerializableExtra("User"); BmobQuery<User> bmobQuery = new BmobQuery<>(); bmobQuery.findObjects(new FindListener<User>() { @Override public void done(List<User> list, BmobException e) { if(e == null){ System.out.println("1"); for (User user1 : list) { //查询到一样 if(user.getAcctount().equals(user1.getAcctount())){ //向云端提交数据 user1.setContext(context); user1.update(user1.getObjectId(), new UpdateListener() { @Override public void done(BmobException e) { if (e == null){ Toast.makeText(First_interface.this, "提交成功", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(First_interface.this, "提交失败", Toast.LENGTH_SHORT).show(); } } }); break; } } } } }); break; } } }
至此,本期项目已经全部实现,尚有不足之处,后续将继续完善补充,欢迎各位下载试玩!文章来源地址https://www.toymoban.com/news/detail-718835.html
到了这里,关于【安卓app开发一】Android Studio + Bmob后端云实现注册&登录账号、密码找回、意见反馈及数据可视化的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!