BaseActivity
public abstract class BaseActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(getLayoutId()); ButterKnife.bind(this); //将当前的activity添加到ActivityManager中 ActivityManager.getInstance().add(this); initTitle(); initData(); } protected abstract void initData(); protected abstract void initTitle(); protected abstract int getLayoutId(); public AsyncHttpClient client = new AsyncHttpClient(); //启动新的activity public void goToActivity(Class Activity,Bundle bundle){ Intent intent = new Intent(this,Activity); //携带数据 if(bundle != null && bundle.size() != 0){ intent.putExtra("data",bundle); } startActivity(intent); } //销毁当前的Activity public void removeCurrentActivity(){ ActivityManager.getInstance().removeCurrent(); } //销毁所有的activity public void removeAll(){ ActivityManager.getInstance().removeAll(); } //保存用户信息 public void saveUser(User user){ SharedPreferences sp = this.getSharedPreferences("user_info", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString("name",user.getName()); editor.putString("imageurl",user.getImageurl()); editor.putBoolean("iscredit", user.isCredit()); editor.putString("phone",user.getPhone()); editor.commit();//必须提交,否则保存不成功 } //读取用户信息 public User readUser(){ SharedPreferences sp = this.getSharedPreferences("user_info", Context.MODE_PRIVATE); User user = new User(); user.setName(sp.getString("name","")); user.setImageurl(sp.getString("imageurl", "")); user.setPhone(sp.getString("phone", "")); user.setCredit(sp.getBoolean("iscredit",false)); return user; } }
用户信息界面:--读取用户
1.判断用户是否已经登录
private void isLogin() { //查看本地是否有用户的登录信息 SharedPreferences sp = this.getActivity().getSharedPreferences("user_info", Context.MODE_PRIVATE); String name = sp.getString("name", ""); if(TextUtils.isEmpty(name)){ //本地没有保存过用户信息,给出提示:登录 doLogin(); }else{ //已经登录过,则直接加载用户的信息并显示 doUser(); } }
2.给出提示:登录
private void doLogin() { new AlertDialog.Builder(this.getActivity()) .setTitle("提示") .setMessage("您还没有登录哦!么么~") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // UIUtils.toast("进入登录页面",false); ((BaseActivity) MeFragment.this.getActivity()).goToActivity(LoginActivity.class, null); } }) .setCancelable(false) .show(); }
3.加载用户信息并显示
private void doUser() { //1.读取本地保存的用户信息 User user = ((BaseActivity) this.getActivity()).readUser(); //2.获取对象信息,并设置给相应的视图显示。 tvMeName.setText(user.getName()); //判断本地是否已经保存头像的图片,如果有,则不再执行联网操作 boolean isExist = readImage(); if(isExist){ return; } //使用Picasso联网获取图片 Picasso.with(this.getActivity()).load(user.getImageurl()).transform(new Transformation() { @Override public Bitmap transform(Bitmap source) {//下载以后的内存中的bitmap对象 //压缩处理 Bitmap bitmap = BitmapUtils.zoom(source, UIUtils.dp2px(62),UIUtils.dp2px(62)); //圆形处理 bitmap = BitmapUtils.circleBitmap(bitmap); //回收bitmap资源 source.recycle(); return bitmap; } @Override public String key() { return "";//需要保证返回值不能为null。否则报错 } }).into(ivMeIcon); //判断一下,是否开启了手势密码。如果开启:先输入手势密码 SharedPreferences sp = this.getActivity().getSharedPreferences("secret_protect", Context.MODE_PRIVATE); boolean isOpen = sp.getBoolean("isOpen", false); if(isOpen){ ((BaseActivity)this.getActivity()).goToActivity(GestureVerifyActivity.class,null); return; } }
登入用户--保存
LoginActivity
@OnClick(R.id.btn_login) public void login(View view){//登录按钮的点击事件 String number = etLoginNumber.getText().toString().trim(); String pwd = etLoginPwd.getText().toString().trim(); if(!TextUtils.isEmpty(number) && !TextUtils.isEmpty(pwd)){ String url = AppNetConfig.LOGIN; RequestParams params = new RequestParams(); params.put("phone",number); params.put("password", MD5Utils.MD5(pwd)); client.post(url,params,new AsyncHttpResponseHandler(){ @Override public void onSuccess(String content) {//200 404 //解析json JSONObject jsonObject = JSON.parseObject(content); boolean success = jsonObject.getBoolean("success"); if(success){ //解析json数据,生成User对象 String data = jsonObject.getString("data"); User user = JSON.parseObject(data, User.class); //保存用户信息 saveUser(user); //重新加载界面 removeAll(); goToActivity(MainActivity.class,null); }else{ Toast.makeText(LoginActivity.this, "用户名不存在或密码不正确", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(Throwable error, String content) { UIUtils.toast("联网失败",false); } }); }else{ UIUtils.toast("用户名或密码不能为空", false); } }