保存登陆username和password
在一些软件中登陆时保存username和password是常见的功能,它实现起来也特别简单,其原理就是在点击登陆button时推断是否勾选保存password选项,假设勾选,则在内存中保存一份包括username和password的文件文件,在下次再打开登陆界面时会获取文件里的信息。
登陆界面:
在onclick中推断假设勾选了记住password:
- if (cb_remeber_password.isChecked()) {
- boolean result = LoginService.saveInfo(this, username, password);
- if(result) {
- Toast.makeText(this, "保存密码成功", 0).show();
- }
saveInfo的方法:
- public static boolean saveInfo(Context context, String username,
- String password) {
- //getFileDir : /data/data/包名/files
- //getCacheDir : /data/data/包名/cache
- File file = new File(context.getFilesDir(), "info.txt");
- try {
- FileOutputStream fos = new FileOutputStream(file);
- fos.write((username + "#" + password).getBytes());
- fos.flush();
- fos.close();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
在这里的getFileDir获取的是手机内存的文件下路径,getCacheDir获取的是应用缓存路径,放在这个路径下的文件会在手机清理缓存是被清理,并且有限制大小,所以一般不建议放在getCacheDir路径下。
这样就保存了一份包括实username和password信息的文件了。下次登录时就能够直接获取这里面的信息而不用又一次输入了
- HashMap<String, String> info = LoginService.getInfo(this);
- if(info != null) {
- et_username.setText(info.get("username"));
- et_password.setText(info.get("password"));
- }
获取登录信息getInfo方法:
- public static HashMap<String, String> getInfo(Context context) {
- File file = new File(context.getFilesDir(), "info.txt");
- try {
- FileInputStream fis = new FileInputStream(file);
- BufferedReader br = new BufferedReader(new InputStreamReader(fis));
- String[] result = br.readLine().split("#");
- HashMap<String, String> map = new HashMap<String, String>();
- map.put("username", result[0]);
- map.put("password", result[1]);
- br.close();
- return map;
- } catch (Exception e) {
- Toast.makeText(context, "无法读取用户信息", 0).show();
- }
- return null;
- }
这样就实现了登录信息的获取
再次登录时的状态:
保存登陆username和password的更多相关文章
- 最终结算“Git Windowsclient保存username与password”问题
Git - How to use netrc file on windows - Stack Overflow 这就是正确答案,我们已经验证过了,以下具体描写叙述一下解决方法: 1. 在Windows ...
- 问题:docker pull 用户登陆tricky,Error response from daemon: unauthorized: incorrect username or password
问题描述: PS C:\WINDOWS\system32> docker pull rabbitmqUsing default tag: latest Please login prior to ...
- 解决git Push时请求username和password,而不是ssh-key验证
转载自:https://blog.lowstz.org/posts/2011/11/23/why-git-push-require-username-password-github/ 之前开始用git ...
- redmine忘记username和password
环境: Ubuntu 13.10 bitnami-redmine-2.5.1-1-linux-x64-installer.run 用bitnami安装完redmine以后,有是否忘记了username ...
- 给新手--安装tomcat后username和password设置以及项目怎么部署在tomcatserver上
安装后tomcatserver后.登陆首先就是让输入username和password.但是我们在安装tomcat的过程中好像没有让设置username和password,这时候可能有人就抓狂了.还有 ...
- Incorrect username or password ( access token )解决
Q:Git提交时,给出提示Incorrect username or password ( access token ) K: 此处是用户名或者密码有误,建议解决方法两种.具体看哪一种可行,可试. 第 ...
- Docker:unauthorized: incorrect username or password.
用VS2017编译DockerCompose项目,显示错误:unauthorized: incorrect username or password. 打开命令行工具,输入docker login命令 ...
- TortoiseGit 连接oschina不用每次输入username和password的方法
每次git clone 和push 都要输入username和password.尽管安全.但在本机上每次都输有些麻烦,怎样记住username和password呢? 在网上看了各种方法,太杂,非常多可 ...
- tomcatserver管理界面username和password忘记
tomcatserverhttp://localhost:8080/ 这样訪问,点击Manager App后要求输入username和password才干进入管理应用界面 我忘记了username和p ...
随机推荐
- Flask实战第55天:cms轮播图上传到七牛功能完成
登录七牛云,进入“对象存储”, 新建存储空间(Bucket), 我创建的空间命名为flask-bbs 创建完Bucket,七牛会给我们提供一个测试域名,生产环境中,我们需要绑定自己的域名 在个人面板中 ...
- WebService数据示例
通过webservice提交xml数据以及soap协议的使用 上次已经给大家分享了简单的webservice的使用,提交给服务器的数据只是简单类型的数据,这次呢换成了xml,并通过一个小例子来讲解so ...
- CSS 笔记——盒子模型
2. 盒子模型 在CSS眼中,任何HTML标签对象都是一个矩形,有长度,宽度以及各角的定位坐标,俗称CSS 盒子模型理论. Margin(外边距) - 清除边框外的区域,外边距是透明的. Border ...
- POJ 1830 开关问题(Gauss 消元)
开关问题 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7726 Accepted: 3032 Description ...
- 【思维】Stacks of Flapjacks
[UVa120] Stacks of Flapjacks 算法入门经典第8章8-1 (P236) 题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列. 试题分析:从插入排序即可找到 ...
- 【字符串哈希】【哈希表】Aizu - 1370 - Hidden Anagrams
给你两个4k长度的串,问你最长公共子串.两个子串相同被定义为所有字母的出现次数分别相同即可. 就枚举第一个串的所有子串,将字母出现的次数看作一个大数,进行哈希(双关键字),塞到哈希表里面.然后枚举第二 ...
- Problem F: 零起点学算法85——数组中插入一个数
#include<stdio.h> int main() { ],b[]; while(scanf("%d",&n)!=EOF) { ;i<n;i++) ...
- linux shell实现随机数多种方法(date,random,uuid)
参考: http://www.cnblogs.com/chengmo/archive/2010/10/23/1858879.html $ cat /proc/sys/kernel/random/uui ...
- Xcode8从相册选图片
使用Xcode8写自己的东西有一段时间了,在使用Xcode8编程时不得不说我特别喜欢改后的默认字体,哈哈,当然默认字体是可以调回去的,只不过默认的字体感觉看起来比以前舒服了,毕竟不会像之前那么”字正腔 ...
- [Linux] Proc 文件系统
转载自:http://linux.chinaunix.net/doc/2004-10-05/16.shtml#324lfindex0 目录: /proc --- 一个虚拟文件系统 加载 proc 文件 ...