Android 模拟登陆 保存密码(信息)到手机中 文件信息读取
package com.wuyou.login; import java.io.IOException;
import java.util.Map; import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast; import com.wuyou.service.LoginService; public class MainActivity extends Activity { private CheckBox checkBox;
private EditText usernamEditText;
private EditText passwordeEditText; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); checkBox = (CheckBox) this.findViewById(R.id.cb);
usernamEditText = (EditText) this.findViewById(R.id.username);
passwordeEditText = (EditText) this.findViewById(R.id.password); // 将之前保存在本地的账号密码取出并设置到文本框中
try {
Map<String, String> map = LoginService.getInfo(this);
usernamEditText.setText(map.get("username"));
passwordeEditText.setText(map.get("password")); } catch (Exception e) {
Log.i("main", "取不出账号密码");
} } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} /**
* 点击“登陆”按钮触发的时间
*
* @param v
*/
public void login(View v) {
Log.i("login", "在登陆中");
String username = usernamEditText.getText().toString().trim();
String password = passwordeEditText.getText().toString().trim();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
Toast.makeText(this, "用户名和账号不能为空!", Toast.LENGTH_SHORT).show();
} else {
// 判断是否选择了记住密码
if (checkBox.isChecked()) {
try {
LoginService.saveInfo(this, username, password);
Toast.makeText(this, "记住密码成功!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "无法记住密码", Toast.LENGTH_SHORT).show();
}
}
if ("zhangsan".equals(username) && "123".equals(password)) {
Toast.makeText(this, "登陆成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "账号密码有误!", Toast.LENGTH_SHORT).show();
}
} } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入账号" /> <EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入密码" /> <EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="记住密码" /> <!-- 记住login的方法有一个参数View -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="login"
android:text="登陆" />
</RelativeLayout> </LinearLayout>
package com.wuyou.service; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map; import android.content.Context; public class LoginService { public static void saveInfo(Context context, String username,
String password) throws IOException {
File file = new File(context.getFilesDir(), "info.txt");
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(username + "##" + password);
fileWriter.close();
} public static Map<String, String> getInfo(Context context)
throws IOException {
//这里只做简单的保存,如果要保存配置文件信息,可以使用SharedPreference类保存,请关注本类别的该文章
File file = new File(context.getFilesDir(),"info.txt");
Map<String, String> map = new HashMap<String, String>();
BufferedReader bf = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
String info = bf.readLine();
String username = "";
String password = "";
if (info != null) {
String[] infos = info.split("##");
username = infos[0];
password = infos[1];
map.put("username", username);
map.put("password", password); }
bf.close();
return map;
}
}
Android 模拟登陆 保存密码(信息)到手机中 文件信息读取的更多相关文章
- Android忘记锁屏密码如何进入手机?
Android忘记锁屏密码如何进入手机? 1.关闭手机 2.进入recovery模式(即恢复模式,记住不是挖煤模式.进入恢复模式不同手机有不同方法,三星的话安主页键,关机键和音量+(或-键), ...
- Android学习笔记——保存数据到SQL数据库中(Saving Data in SQL Databases)
知识点: 1.使用SQL Helper创建数据库 2.数据的增删查改(PRDU:Put.Read.Delete.Update) 背景知识: 上篇文章学习了保存文件,今天学习的是保存数据到SQL数据库中 ...
- python保存二维列表到txt文件,读取txt文件里面的数据转化为二维列表
源码: # 读文件里面的数据转化为二维列表 def Read_list(filename): file1 = open(filename+".txt", "r" ...
- 【ASP.NET 进阶】获取MP3文件信息并显示专辑图片
突发奇想,想弄个显示MP3文件信息和专辑图片的小Demo,个人不是大牛,遂百度之,总算搞定,现分享如下. 效果图: GIF效果图: 主要是依靠2个DLL文件:ID3.dll 和 Interop.She ...
- Android——用户登陆及用户名和密码的保存
Android——用户登陆及用户名和密码的保存 在之前的学习过程中已经将Android学习完了,但是在后面将近一年的时间里都没有进行过Android开发,所以对Android的所有的知识点又有点忘 ...
- [android] 手机卫士保存密码时进行md5加密
一般的手机没有root权限,进不去data/data目录,当手机刷机了后,拥有root权限,就可以进入data/data目录,查看我们保存的密码文件,因此我们需要对存入的密码进行MD5加密 获取Mes ...
- HttpClient+Jsoup模拟登陆贺州学院教务系统,获取学生个人信息
前言 注:可能学校的教务系统已经做了升级,当前的程序不知道还能不能成功获取信息,加上已经毕业,我的账户已经被注销,试不了,在这里做下思路跟过程的记录. 在我的毕业设计中”基于SSM框架贺州学院校园二手 ...
- Android模拟位置信息
Android模拟位置程序,俗称GPS欺骗,只能修改采用GPS定位的软件. 手机定位方式目前有4种:基站定位,WIFI定位,GPS定位,AGPS定位 常见的修改手法: 1. 抓包欺骗法,抓包改包欺骗服 ...
- 让 Putty 保存密码,自动登陆的四种方法
Putty 基本是我在紧急时候用来登陆 Linux/Unix 终端的不二之先,因其小,开源,界面也非常实用.可是当你要在私有的机器上,经常性的要登陆很多机器的时候就觉得烦琐了,不光打开一堆的窗口,还要 ...
随机推荐
- Windows redis集群搭建
一.Windows下Redis安装 下载地址https://github.com/dmajkic/redis/downloads,下载到的Redis支持32bit和64bit.根据自己实际情况选择,本 ...
- "ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源, 或者超时失效"的快速解决方法
引自:http://hi.baidu.com/fynaa/item/c2978952d8d542dfd48bacf6 讲了一大堆: 综合下: 解决方案:select session_id from v ...
- Socket的3次握手链接与4次断开握手
http://blog.sina.com.cn/s/blog_810c860001018tir.html 连接握手: 1.客户端发送建立连接请求 2.服务端确认连接请求 3.客户端确认已经连接 以上3 ...
- sublime text3输入中文的问题.
1.新建sublime_imfix.c文件 里面输入: /* * sublime-imfix.c * Use LD_PRELOAD to interpose some function to fix ...
- 什么情况下会调用到session_destroy()
https://segmentfault.com/q/1010000000191102 首先 ... session_destory() 是一个函数 ... 这个函数在任何情况下都不会被 php 引擎 ...
- ArcGIS Runtime SDK for WPF已不更新,后续将被ArcGIS Runtime SDK for .NET取代
ArcGIS Runtime SDK 10.2.5 for WPF is now available! by mbranscomb and Rex Hansen on January 27, 2015 ...
- Java—static、this、super用法总结
通过用static来定义方法或成员,为我们编程提供了某种便利,从某种程度上可以说它类似于C语言中的全局函数和全局变量.(理解为加了static的就是全局变量)但是,并不是说有了这种便利,你便可 ...
- Struts2的运行原理和运行与原理
Struts2 struts2的流程图 运行机制 1.客户端发送请求.通过ActionContextLoader调用FilterDispatcher(struts) 2.FilterDispatche ...
- 【CF493E】【数学】Vasya and Polynomial
Vasya is studying in the last class of school and soon he will take exams. He decided to study polyn ...
- LINUX开机启动过程
LINUX开机启动过程 启动第一步--加载BIOS当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息 ...