保存登陆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 ...
随机推荐
- Java基于数据源的数据库访问
☞ 概述 最早接触的Java访问数据库,是通过jdbc接口.后来工作之后,一般是在服务器(如weblogic)配置数据源,通过JNDI使用数据源:最近需要在程序中动态构造数据源,查了些资料,备录于此. ...
- Maven实用总结
使用Maven还是推荐IDEA,以前用eclipse总是喜欢出现乱七八糟的问题,具体错误和解决方案也记不清楚了. 下面总结下IDEA中遇到的问题和解决方法: 与IDEA搭配的相关问题 如何根据模板快速 ...
- 洛谷——P1692 部落卫队
题目描述 原始部落byteland中的居民们为了争夺有限的资源,经常发生冲突.几乎每个居民都有他的仇敌.部落酋长为了组织一支保卫部落的队伍,希望从部落的居民中选出最多的居民入伍,并保证队伍中任何2 个 ...
- Aizu 2784 Similarity of Subtrees(树哈希)
Similarity of Subtrees Define the depth of a node in a rooted tree by applying the following rules r ...
- Visual Studio 2017启动x86的Android模拟器失败
Visual Studio 2017启动x86的Android模拟器失败 Visual Studio 2017默认提供多个Android模拟器.其中,x86模拟器运行较快.但是由于和Hyper-V服 ...
- 【最短路】【dijkstra】【二进制拆分】hdu6166 Senior Pan
题意:给你一张带权有向图,问你某个点集中,两两结点之间的最短路的最小值是多少. 其实就是dijkstra,只不过往堆里塞边的时候,要注意塞进去它是从集合中的哪个起始点过来的,然后在更新某个点的答案的时 ...
- 【插头dp】CDOJ1690 这是一道比CCCC简单题难的简单题
最裸的插头dp,可参见大白书. #include<cstdio> #include<cstring> using namespace std; #define MOD 1000 ...
- Problem I: 零起点学算法88——青年歌手大奖赛_评委会打分
#include<stdio.h> int main(void) { ],n,i; while(scanf("%d",&n)!=EOF) { n>& ...
- Ubuntu 16.04安装RedisDesktopManager
说明:0.9版本的安装补上,只能安装0.8版本的. 官网: https://github.com/uglide/RedisDesktopManager 下载: https://github.com/u ...
- 嵌入式学习之Nand Flash
转:http://m.blog.csdn.net/blog/woshixiongge/9017149 Nand Flash是flash存储器的一种,其内部采用非线性宏单元模式,为固态大容量内存的实现提 ...