1.Register Activity: Also launches an intent to specific activities according to user status. But when a new account is created on a different device all the devices logs in to the latest account created.
if(NetworkConnection.isConnectedToInternet(getApplicationContext())){
switch(v.getId()){
case R.id.cancel:
this.finishAffinity();
break;
case R.id.Re:
String name = edit_rename.getText().toString().trim();
String email = edit_reemail.getText().toString().trim();
String password = edit_repwd.getText().toString().trim();
if(TextUtils.isEmpty(name)){
Toast.makeText(RegisterActivity.this,"Please enter username",Toast.LENGTH_SHORT).show();
break;
}
if(TextUtils.isEmpty(email)){
Toast.makeText(RegisterActivity.this,"Please enter email",Toast.LENGTH_SHORT).show();
break;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(RegisterActivity.this,"Please enter password",Toast.LENGTH_SHORT).show();
break;
}
if(rbGrp.getCheckedRadioButtonId() == -1){
Toast.makeText(RegisterActivity.this,"Please Select Status",Toast.LENGTH_SHORT).show();
break;
}
if(edit_rename.getText().toString().trim().length() != 0 && edit_reemail.getText().toString().trim().length() != 0&&
edit_repwd.getText().toString().trim().length() != 0 && rb1.isChecked() || rb2.isChecked() || rb3.isChecked()){
AVUser user= new AVUser();
user.setUsername(name);
user.setEmail(email);
user.setPassword(password);
if(rb1.isChecked()){
user.put("Status","Administrator");
}else if(rb2.isChecked()){
user.put("Status","Lecturer");
}else {
user.put("Status","Student");
}
user.signUpInBackground(new SignUpCallback() {
@Override
public void done(AVException e) {
if(e==null){
if(rb1.isChecked()){
// clientId 为 Tom
AVIMClient avimClient = AVIMClient.getInstance(edit_rename.getText().toString());
avimClient.open(new AVIMClientCallback() {
@Override
public void done(AVIMClient avimClient, AVIMException e) {
Intent i = new Intent(RegisterActivity.this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
});
}else if(rb2.isChecked()){
// clientId 为 Tom
AVIMClient avimClient = AVIMClient.getInstance(edit_rename.getText().toString());
avimClient.open(new AVIMClientCallback() {
@Override
public void done(AVIMClient client, AVIMException e) {
Intent i = new Intent(RegisterActivity.this,LecturerMainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
});
}else {
// clientId 为 Tom
AVIMClient avimClient = AVIMClient.getInstance(edit_rename.getText().toString());
avimClient.open(new AVIMClientCallback() {
@Override
public void done(AVIMClient client, AVIMException e) {
Intent i = new Intent(RegisterActivity.this,StudentMainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
});
}
}
else{
Toast.makeText(RegisterActivity.this,"账号已经存在,请重新输入",Toast.LENGTH_SHORT).show();
}
}
});
}
break;
case R.id.to_logpg_txt:
Intent intent2=new Intent(this,LogInActivity.class);
startActivity(intent2);
break;
default:break;
}
}else
Toast.makeText(RegisterActivity.this,"No Connection Available",Toast.LENGTH_SHORT).show();
}
2.Log In: this is code for logging in as i am accessing different activities due to the status of the user
if(NetworkConnection.isConnectedToInternet(getApplicationContext())){
switch (v.getId()){
case R.id.login:
String name_email=edit_name.getText().toString().trim();
String password=edit_pwd.getText().toString().trim();
if(TextUtils.isEmpty(name_email)){
Toast.makeText(LogInActivity.this,"Please enter username",Toast.LENGTH_SHORT).show();
break;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(LogInActivity.this,"Please enter password",Toast.LENGTH_SHORT).show();
break;
}
if(!TextUtils.isEmpty(name_email) && !TextUtils.isEmpty(password)){
AVUser.logInInBackground(name_email, password, new LogInCallback<AVUser>() {
@Override
public void done(AVUser avUser, AVException e) {
if(e==null){
AVQuery<AVObject> user_status_query = new AVQuery<>("_User");
user_status_query.findInBackground(new FindCallback<AVObject>() {
@Override
public void done(List<AVObject> list, AVException e) {
for(AVObject avObject:list){
String status_n= (String) avObject.get("Status");
if(status_n.equals("Administrator")){
// 与服务器连接
Intent i = new Intent(LogInActivity.this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
else if(status_n.equals("Lecturer")){
Intent i = new Intent(LogInActivity.this,LecturerMainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}else if(status_n.equals("Student")){
Intent i = new Intent(LogInActivity.this,StudentMainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
}
}
});
// 执行其他逻辑
}
else{
Toast.makeText(LogInActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
}
}
});
}
break;
case R.id.register:
Intent intent=new Intent(this,RegisterActivity.class);
startActivity(intent);
break;
case R.id.forget:
break;
}
}else
Toast.makeText(LogInActivity.this,"No Connection Available",Toast.LENGTH_SHORT).show();
}