前一阶段看徐宜生写的Android群英传里面介绍的搭建云端服务器,那个时候自己不懂,加上有其他的学习,就没有深入,但是这两天找了资料研究了一下,读了官方文档,感觉挺不错的,服务端不用自己搭建了。(听说集成了好多东西,可以聊天、支付、分享、注册什么的)那就自己模拟一下注册登录吧。
注册完成并且创建好项目后,点击设置,就会看到这个,在这里可以获取到用于初始化的AppLication ID
其实很简单,在这里我直接上代码,因为Bmob已经为我们写好了后台,对于数据库的增删改查都已经封装好了,我们只是简单的调用就行(必要的解释我写在注释里面)
User.java:
public class User extends BmobUser {
//这个BmobFile是特有的,我们可以用来上传我们的图片(头像资源)
private BmobFile icon;
public BmobFile getIcon() {
return icon;
}
public void setIcon(BmobFile icon) {
this.icon = icon;
}
}
LoginActivity.java:
package com.example.wuyinlei.mybomb;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
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 LoginActivity extends AppCompatActivity implements View.OnClickListener {
private EditText etusername;
private EditText etpassword;
private Button login;
private Button sign;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//这里的AppLication ID 写上自己创建项目得到的那个AppLication ID
Bmob.initialize(this, "Application ID");
initialize();
initView();
}
private void initView() {
}
private void initialize() {
etusername = (EditText) findViewById(R.id.et_username);
etpassword = (EditText) findViewById(R.id.et_password);
login = (Button) findViewById(R.id.login);
login.setOnClickListener(this);
sign = (Button) findViewById(R.id.sign);
sign.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.login:
final String username = etusername.getText().toString();
String password = etpassword.getText().toString();
if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)){
final BmobUser bmobUser = new BmobUser();
bmobUser.setUsername(username);
bmobUser.setPassword(password);
bmobUser.login(LoginActivity.this, new SaveListener() {
@Override
public void onSuccess() {
//获取到当前用户的信息
User user = BmobUser.getCurrentUser(LoginActivity.this,User.class);
Intent intent = new Intent(LoginActivity.this,MainInfoActivity.class);
intent.putExtra("user",user);
startActivity(intent);
//登录成功
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int i, String s) {
}
});
}
break;
case R.id.sign:
Intent intent = new Intent(this,RegisterActivity.class);
startActivity(intent);
break;
}
}
}
activity_login.xml:
<?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.example.wuyinlei.mybomb.LoginActivity">
<EditText android:id="@+id/et_username" android:hint="登录名" android:layout_width="match_parent" android:layout_height="wrap_content" />
<EditText android:id="@+id/et_password" android:hint="密码" android:layout_width="match_parent" android:layout_height="wrap_content" />
<LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:id="@+id/login" android:layout_width="0dp" android:layout_weight="1" android:text="登录" android:layout_height="wrap_content" />
<Button android:id="@+id/sign" android:text="注册" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
RegisterActivity.java:
package com.example.wuyinlei.mybomb;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadFileListener;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private EditText etusername;
private EditText etpassword;
private EditText etemail;
private EditText eticon;
private Button register;
private Button cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
initialize();
}
private void initialize() {
etusername = (EditText) findViewById(R.id.et_username);
etpassword = (EditText) findViewById(R.id.et_password);
etemail = (EditText) findViewById(R.id.et_email);
register = (Button) findViewById(R.id.register);
register.setOnClickListener(this);
cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.register:
registerData();
break;
case R.id.cancel:
break;
}
}
/** * 注册 */
private void registerData() {
final String name = etusername.getText().toString();
final String password = etpassword.getText().toString();
final String email = etemail.getText().toString();
String path ;
//获取图片文件的路径(我在个文件路径下面放了一个1.jpg图片)
path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/1.jpg";
final BmobFile bmobFile = new BmobFile(new File(path));
bmobFile.upload(this, new UploadFileListener() {
@Override
public void onSuccess() {
//上传成功
User user = new User();
user.setUsername(name);
//user.setIcon(icon);
user.setEmail(email);
user.setPassword(password);
user.setIcon(bmobFile);
//注册(这个是已经提供好的接口)
user.signUp(RegisterActivity.this, new SaveListener() {
@Override
public void onSuccess() {
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int i, String s) {
Toast.makeText(RegisterActivity.this, s.toString(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onFailure(int i, String s) {
Log.d("RegisterActivity", "报错了" + s.toString());
}
});
}
}
activity_register.xml:
<?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:orientation="vertical" android:layout_height="match_parent" tools:context="com.example.wuyinlei.mybomb.RegisterActivity">
<EditText android:id="@+id/et_username" android:hint="登录名" android:layout_width="match_parent" android:layout_height="wrap_content" />
<EditText android:id="@+id/et_password" android:hint="密码" android:layout_width="match_parent" android:layout_height="wrap_content" />
<EditText android:id="@+id/et_email" android:hint="邮箱" android:layout_width="match_parent" android:layout_height="wrap_content" />
<LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:id="@+id/register" android:layout_width="0dp" android:layout_weight="1" android:text="注册" android:layout_height="wrap_content" />
<Button android:id="@+id/cancel" android:text="取消" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
MainInfoActivity.java:
package com.example.wuyinlei.mybomb;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;
import cn.bmob.v3.datatype.BmobFile;
public class MainInfoActivity extends AppCompatActivity {
private TextView etusername;
private ImageView icon;
private User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
user = (User) getIntent().getSerializableExtra("user");
initialize();
}
private void initialize() {
etusername = (TextView) findViewById(R.id.et_username);
icon = (ImageView) findViewById(R.id.icon);
etusername.setText(user.getUsername());
BmobFile file = user.getIcon();
//加载全图
//file.loadImage(this,icon);
//加载缩略图 自己定义大小和质量第三个第四个参数是我们要加载图片显示的大小,第五个是显示的质量(100最好)
file.loadImageThumbnail(this,icon,100,100,50);
}
}
main_info_activity.xml:
<?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=".MainInfoActivity">
<TextView android:textSize="24dp" android:id="@+id/et_username" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
基本上所有的代码都在上面了,这里需要解释的一个,User表,在使用的时候自动创建,然后我们可以使用它暴漏出来的接口去增删改查,实际上我们的模拟登陆就是使用了增、查、显示我们查到的数据。我们来看下实现的效果:
这里我们看到基本的逻辑都实现了,只是界面有待优化哈。好了,今天就到这里了,如果后期有时间,会对Bmob有点讲解。后面会有一个关与基于Bmob的使用完成的便签功能(也是很简单实现的)。