The easiest way to log in and register in history (Bomb backend cloud)
It’s been almost a year since I’ve been learning Android development. I’ve written many, many times to log in and register. I feel like it takes a long time to write each time, so I’m thinking about how to make it easier. I saw a few great gods use the Bmod backend cloud to do this login and registration. At first, when I saw the name, I felt that it was high-end, so I went to learn it with the mentality of giving it a try. Sure enough, it is really simple, no Say more about the picture above
First, register an account on Bmob: https://www.bmob.cn/login
Then create an application in it, please see the development documentation later
Not much to say, on the code
log in
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{ TextView tv_register; EditText et_name,et_password; Button btn_login; @Override protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); Bmob.initialize(this, "9d2b932612d7936e4cad1e81168b074d"); tv_register=(TextView)findViewById(R.id.tv_register); btn_login=(Button)findViewById(R.id. btn_login); et_name=(EditText)findViewById(R.id.et_login_username); et_password=(EditText)findViewById(R.id.et_login_password); tv_register.setOnClickListener(this); btn_login.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.tv_register: Intent intent=new Intent(LoginActivity.this,RegistrationActivity.class); startActivity(intent); break; case R.id.btn_login: String name = et_name.getText().toString(); String password = et_password.getText().toString().trim(); if(name.equals("")||password.equals("")){ Toast. makeText (LoginActivity. this, "The account password cannot be empty" , Toast. LENGTH_SHORT ).show() ; return; } BmobUser user=new BmobUser(); user.setUsername(name); user.setPassword(password); user.login(new SaveListener<BmobUser>() { @Override public void done(BmobUser bmobuser, BmobException e) { if(e==null){ Intent intent=new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); }else{ Toast. makeText (LoginActivity. this, "Login failed" , Toast. LENGTH_SHORT ).show() ; } } }); break; } } }
User class inherits BmobUser
Can write here or not
/** * Created by Administrator on 2017/5/14. */ public class User extends BmobUser { }
register this piece
public class RegistrationActivity extends AppCompatActivity { EditText edit_name, edit_password, edit_the_second_password; Button btn_registration; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_regisration); Bmob.initialize(this, "9d2b932612d7936e4cad1e81168b074d"); edit_name = (EditText) findViewById(R.id.edit_name); edit_password = (EditText) findViewById(R.id.edit_password); edit_the_second_password = (EditText) findViewById(R.id.edit_the_second_password); btn_registration = (Button) findViewById(R.id.btn_registration); btn_registration.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String username = edit_name.getText().toString(); String password = edit_password.getText().toString(); if(username.equals("")||password.equals("")){ Toast.makeText(RegistrationActivity.this, "输入不能为空", Toast.LENGTH_SHORT).show(); return; } BmobUser user=new BmobUser(); user.setUsername(username); user.setPassword(password); user.signUp(new SaveListener<BmobUser>(){ @Override public void done(BmobUser s, BmobException e) { if(e==null){ Intent intent=new Intent(RegistrationActivity.this,LoginActivity.class); startActivity(intent); }else{ Toast.makeText(RegistrationActivity.this, "注册失败", Toast.LENGTH_SHORT).show(); } } }); } }); } }