hoge66的专栏

-----------------------看到专栏,偷着笑!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

android项目杂记

Posted on 2017-01-15 09:52  hehoge  阅读(229)  评论(0编辑  收藏  举报

1、自动登录,在SdCrmApplication 代码中,

 String username = String.valueOf(SpUtils.get(this, SharePrefUtil.KEY.USER_NAME, ""));
            String password = String.valueOf(SpUtils.get(this, SharePrefUtil.KEY.USER_PASSWORD, ""));

 2、记录用户信息,在登录后LoginActivity

 protected void saveUserInfor(Users users, String password) {
        app.setUsers(users);
        try {
            password= AesUtil.encrypt(password,JxConf.AESKEY);
            SpUtils.put(SharePrefUtil.KEY.USER_PASSWORD, password);
            SpUtils.put(SharePrefUtil.KEY.USER_NAME, users.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

 3、退出登录 UserInformationActivity

	build.setPositiveButton("确定", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// 右盟分析工具
				//	MobclickAgent.onEvent(ct, JxConf.C_MYCOFFERS_PERSONALINFORMATION_DROPOUT);
					removePushTag();
					app.setUsers(null);
					app.setUserInfo(null);

					SpUtils.remove(ct,SharePrefUtil.KEY.USER_NAME);

					SharePrefUtil.saveLong(ct, SharePrefUtil.KEY.LAST_UID, 0);
					AppManager appManager = AppManager.getAppManager();
					MainActivity activity = (MainActivity) appManager.indexOfActivity(0);
					activity.getHomeFragment().settingMessage(0);
					// 通知服务器登出
					if (userService == null) {
						userService = new UserService(ct);
					}
					userService.logout(users.getUsername(),new TextHttpResponseHandler(){
						@Override
						public void onFailure(int statusCode, Header[] headers,
								String responseBody, Throwable error) {
							super.onFailure(statusCode, headers, responseBody,
									error);
							setResult(RESULT_OK);
							finish();
						}

						@Override
						public void onSuccess(int statusCode, Header[] headers,
								String responseBody) {
							super.onSuccess(statusCode, headers, responseBody);
							setResult(RESULT_OK);
							finish();
						}
					});
					activity.remoteGetAccessToken();
					activity.getHomeFragment().selectPageView(R.id.rb_home);
				}
			});

 4、首次使用自动登录的地方MainActivity

@Override
    protected void onResume() {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(
                curTag);
        if (fragment != null) {
            fragment.onResume();
        }
        super.onResume();
        // resumeStatus = isAppOnForeground();

        if (app.getUsers() != null && app.getUsers().getUsername()!="") {
            if (stopStatus == true && isActive == false) {

                boolean switcher = SharePrefUtil.getBoolean(ct,
                        SharePrefUtil.KEY.SWITCH_GESTURE, false);
                if (switcher == true) {
                    isActive = true;// app 从后台唤醒,进入前台
                    stopStatus = true;
                    long intoTime=SharePrefUtil.getLong(ct,SharePrefUtil.KEY.OUT_TIME,0);
                    if(System.currentTimeMillis()-intoTime>= Constants.INTOTIMELOCKED) {
                        // 暂时不提供锁频界面
                       /* Intent intent = new Intent(this, LockActivity.class);
                        startActivity(intent);*/
                    }
                }

            }
        }


    }