Toast工具
public class MyToast {
public static void makeText(Context context, CharSequence text){
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
}
1.1 Toast工具
final User user = new User(username, password,"138604167");
user.signUp(getActivity(), new SaveListener() {
public void onSuccess() {
MyToast.makeText(getActivity(),"注册成功:" + user.getUsername() + "-" + user.getObjectId()
+ "-" + user.getCreatedAt() + "-"
+ user.getSessionToken() + ",是否验证:"
+ user.getEmailVerified());
2. 判断登录状态工具
private static LoginStatus mInstance = new LoginStatus();
private boolean hasLogin;
private LoginStatus(){}
public static LoginStatus getInstance(){
return mInstance;
}
public boolean hasLogin() {
return hasLogin;
}
public void setHasLogin(boolean hasLogin) {
this.hasLogin = hasLogin;
}
}
2.1 注册成功后设置登录状态为true,再跳转
myUser = BmobUser.getCurrentUser(getActivity(), com.chencheng.model.User.class);
Log.e("life","本地用户信息:objectId = " + myUser.getObjectId() + ",name = " + myUser.getUsername()
);
LoginStatus.getInstance().setHasLogin(true);
Intent intent = new Intent(getActivity(),
MainActivity.class);
intent.putExtra("user", myUser);
intent.putExtra("flag", "hasLogin");
startActivity(intent);
2.2 MainActivity中接受Intent值,如果是登陆成功,直接条状到MineFragment
Intent intent=getIntent();
flag = intent.getStringExtra("flag");
if(flag!=null){
Log.e("MainIntent", flag);
if(flag.equals("hasLogin")){
mTabHost.setCurrentTab(2);
}
}
2.3 MineFragment里面根据登录状态设置不同显示界面
if (LoginStatus.getInstance().hasLogin()){
com.chencheng.model.User myUser = BmobUser.getCurrentUser(getActivity(), com.chencheng.model.User.class);
TextView username=(TextView)mLayout.findViewById(R.id.username);
username.setText(myUser.getUsername());
mLayout.findViewById(R.id.has_not_login_container).setVisibility(View.GONE);
mLayout.findViewById(R.id.logined_container).setVisibility(View.VISIBLE);
}else{
mLayout.findViewById(R.id.has_not_login_container).setVisibility(View.VISIBLE);
mLayout.findViewById(R.id.logined_container).setVisibility(View.GONE);
}
3.跳转工具(基于判断登录状态)
public class UnLoginUtils {
public static void startActivity(Context context, Class class1){
if (LoginStatus.getInstance().hasLogin()){
context.startActivity(new Intent(context, class1));
}else{
context.startActivity(new Intent(context, LoginActivity.class));
}
}
}
转载于:https://blog.51cto.com/1137907860/1672738