Android实践——使用Bmob实现登录、注册等功能

Bmob云可以用于云端数据库,使用方便简单,只需要我们在Bmob云注册之后,获取产品apk即可,下面代码就简单介绍了一下

我们可以写一个User类,包含name、password

import cn.bmob.v3.BmobObject;
import cn.bmob.v3.listener.UpdateListener;


public class User extends BmobObject{

    private String name;
    private String password;
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

	

}
MainActivity类

import java.util.List;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.DeleteListener;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
	
	private Button btnlogin;
	
	private Button btnregister;
	
	private Button btndelete;
	
	private Button btnupdate;
	
	private EditText txtusername;
	
	private EditText txtpassword;
	
	private String ObjectId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bmob.initialize(this, "5f1b3e34ff5b6d3f0896d7acdbfbd64f");//初始化BmobSDK
        
        txtpassword = (EditText) findViewById(R.id.edit_password);
        txtusername = (EditText) findViewById(R.id.edit_username);
        btnlogin =(Button)findViewById(R.id.btn_login);
        btnregister = (Button)findViewById(R.id.btn_register);
        btnupdate = (Button)findViewById(R.id.btn_update);
        btndelete = (Button)findViewById(R.id.btn_delete);
        
        btnlogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				doLogin();
			}
		});
        
        btnregister.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				doRegister();
			}
		});
        
        btndelete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				doDelete();
			}
		});
        
        btnupdate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				doUpdate();
			}
		});
    }
    
    private void doRegister(){//注册
    	BmobQuery query=new BmobQuery();
    	User user = new User();
    	user.setName(txtusername.getText().toString());
    	user.setPassword(txtpassword.getText().toString());
    	user.save(MainActivity.this, new SaveListener() {
			
			@Override
			public void onSuccess() {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this,"register success",3*1000).show();
			}
			
			@Override
			public void onFailure(int arg0, String arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, arg1, 3*1000).show();
			}
		});
    }
    
    
    private void doLogin(){//登录
   	 BmobQuery query=new BmobQuery();
     String userName=txtusername.getText().toString();
     //String userName="123";
     query.addWhereEqualTo("name",userName);//增加查询条件
     query.setLimit(2);
     query.findObjects(MainActivity.this, new FindListener() {

		@Override
		public void onError(int arg0, String arg1) {
			// TODO Auto-generated method stub
			Toast.makeText(MainActivity.this, arg1,20*1000).show();
		}

		@Override
		public void onSuccess(List list) {
			// TODO Auto-generated method stub
			User user=list.get(0);//获取User对象
			if(user.getPassword().equals(txtpassword.getText().toString())){
				Toast.makeText(MainActivity.this, "login success",3*1000).show();
				ObjectId=list.get(0).getObjectId();
			}
			else{
				Toast.makeText(MainActivity.this, "password error",3*1000).show();
			}
			
		}
     });
    }
    
    
    private void doUpdate(){//更新密码
    	User user =new User();
    	user.setObjectId(ObjectId);
    	user.setPassword(txtpassword.getText().toString());
    	user.update(MainActivity.this, new UpdateListener() {
			
			@Override
			public void onSuccess() {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "update success",3*1000).show();
			}
			
			@Override
			public void onFailure(int arg0, String arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, arg1,3*1000).show();
			}
		});
    }
    
    private void doDelete(){//删除
    	User user = new User();
    	user.setObjectId(ObjectId);
    	user.delete(MainActivity.this, new DeleteListener() {
			
			@Override
			public void onSuccess() {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "delete success",3*1000).show();
			}
			
			@Override
			public void onFailure(int arg0, String arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, arg1,3*1000).show();
			}
		});
    }
    
}

注意在AndroidManifest中添加权限
Bmob后端


你可能感兴趣的:(Android)