Android:Day03_完美登录案例(使用流和文件来保存登录信息)
Day03_完美登录案例
目录
Day03_完美登录案例
一、项目目录结构
二、类和文档(按项目目录结构排序)
1、类:LoginSace
2、类:MainActivity
3、AndroidManifest.xml
4、activity_main.xml
一、项目目录结构
二、类和文档(按项目目录结构排序)
1、类:LoginSace
package com.example.loginperfect;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;import android.content.Context;
import android.os.Environment;
import android.provider.MediaStore.Video;
import android.view.View;
import android.widget.Toast;public class LoginSace {//1.保存用户信息到手机T卡public static boolean saveinfobycontext(String username, String pwd){//1.将信息合并为一整条字符串信息String infostr=username+"##"+pwd;File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tcardinfo.txt");//2.得到一个输出流try {FileOutputStream fos=new FileOutputStream(file);fos.write(infostr.getBytes());fos.close();return true;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return false;}}//1.读取用户信息到手机T卡public static String readinfofromTcard(){File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tcardinfo.txt");//2.得到一个输出流try {FileInputStream fis=new FileInputStream(file);BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis));String readLine = bufferedReader.readLine();return readLine;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return null;}}//2.保存用户信息到手机内部路径public static boolean saveinfobycontext(Context context, String username, String pwd){//1.将信息合并为一整条字符串信息String infostr=username+"##"+pwd;FileOutputStream fos;//File file = new File(context.getFilesDir().getAbsolutePath()+"/innerinfo.txt");//2.得到一个输出流try {// fos=new FileOutputStream(file);fos=context.openFileOutput("innerinfo.txt", Context.MODE_PRIVATE);fos.write(infostr.getBytes());fos.close();return true;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return false;}}//2.保存用户信息到手机内部路径public static String readinfobycontext(Context context){//File file = new File(context.getFilesDir().getAbsolutePath()+"/innerinfo.txt");//2.得到一个输出流try {// fos=new FileOutputStream(file);FileInputStream fis=context.openFileInput("innerinfo.txt");BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis));String readLine = bufferedReader.readLine();return readLine;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return null;}}
}
2、类:MainActivity
package com.example.loginperfect;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends Activity {private EditText et_username;private EditText et_pwd;private CheckBox cb_issave;private Button bt_login;private String strtcard;private TextView tv_freespace_ex;private TextView tv_totalspace_ex;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_username = (EditText)findViewById(R.id.et_username);et_pwd = (EditText)findViewById(R.id.et_pwd);cb_issave = (CheckBox)findViewById(R.id.cb_issave);bt_login = (Button)findViewById(R.id.bt_login);tv_freespace_ex = (TextView)findViewById(R.id.tv_freespace_ex);tv_totalspace_ex = (TextView)findViewById(R.id.tv_totalspace_ex);File externalStorageDirectory = Environment.getExternalStorageDirectory();//给控件赋值,显示sd卡的剩余空间大小和总空间大小tv_freespace_ex.setText(Formatter.formatFileSize(this, externalStorageDirectory.getFreeSpace()));tv_totalspace_ex.setText(Formatter.formatFileSize(this, externalStorageDirectory.getTotalSpace()));//打开界面即读取信息if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {strtcard = LoginSace.readinfofromTcard();}else {strtcard = LoginSace.readinfobycontext(MainActivity.this);}//读取其它应用的私有文件//判断字符串是否为空if(strtcard != null) {String[] name_pwd = strtcard.split("##");et_username.setText(name_pwd[0]);et_pwd.setText(name_pwd[1]);}//2.设置登录监听事件bt_login.setOnClickListener(new OnClickListener() {private boolean retsave;@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//1.获取username和pwdString username=et_username.getText().toString().trim();String pwd=et_pwd.getText().toString().trim();//2.判断username和pwd是否为空if(TextUtils.isEmpty(username)||TextUtils.isEmpty(pwd)) {Toast.makeText(MainActivity.this, "username or pwd is null!", Toast.LENGTH_SHORT).show();}else {if(cb_issave.isChecked()) {/*保存登录信息*///判断手机T卡是否挂载if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//1.保存在手机T卡存储retsave=LoginSace.saveinfobycontext(username,pwd);}else {//2.保存在手机内部存储retsave=LoginSace.saveinfobycontext(MainActivity.this, username, pwd);}} if(retsave) {Toast.makeText(MainActivity.this, "save successfully!", Toast.LENGTH_SHORT).show();}else {Toast.makeText(MainActivity.this, "save fail!", Toast.LENGTH_SHORT).show();;}}}});}//另一种按钮点击事件,方法名字与android:onClick="btreadotherappfile"一致public void btreadotherappfile(View v) {File file = new File("data/data/com.example.login/files/info.txt");try {BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));String readLine = bufferedReader.readLine();Toast.makeText(this,readLine, Toast.LENGTH_SHORT).show();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();Toast.makeText(this,"读取失败!", Toast.LENGTH_SHORT).show();}}}
3、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.loginperfect"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="17"android:targetSdkVersion="17" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name=".MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application>
</manifest>
4、activity_main.xml
<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: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="com.example.loginperfect.MainActivity" android:orientation="vertical"><EditText android:id="@+id/et_username"android:layout_width="match_parent"android:layout_height="30dp"android:hint="请输入用户名"android:background="#FF0000"/><EditText android:id="@+id/et_pwd"android:layout_width="match_parent" android:layout_height="30dp"android:hint="请输入密码"android:inputType="textPassword"android:background="#00FF00"/><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><CheckBox android:id="@+id/cb_issave"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="是否保存登录信息"/><Button android:id="@+id/bt_login"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="登录"/></LinearLayout><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextView android:id="@+id/tv_freespace"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="剩余空间大小:"/><TextView android:id="@+id/tv_freespace_ex"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:hint="剩余空间大小"/></LinearLayout><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextView android:id="@+id/tv_totalspace"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="总空间大小:"/><TextView android:id="@+id/tv_totalspace_ex"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:hint="总空间大小"/></LinearLayout><LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><Button android:id="@+id/bt_read"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#ff00ff"android:text="读取其它私有文件"android:onClick="btreadotherappfile"/></LinearLayout>
</LinearLayout>
Android:Day03_完美登录案例(使用流和文件来保存登录信息)相关推荐
- html登录界面cookie,HTML中使用cookie保存登录账户
用户点击登录按钮后 判断当前账户是否存在 如果存在 并且勾选了记住帐号 使用cookie保存帐号密码 如果存在 但是没有勾选记住帐号 遍历cookie 删除帐号密码 //判断当前用户是否存在 i ...
- Android中从assets资源中读取图片文件并保存到内部存储器并加载显示在ImageView中
场景 Android系统为每个新设计的程序提供了/assets目录,这个目录保存的文件可以打包在程序里./res和/assets的不同点是,android不为/assets下的文件生成ID.如果使用/ ...
- uniapp 下载文件/二进制流数据文件 ,保存文件 ,打开文件。以及H5下载二进制流文件。
前提注意点 downloadFile 发送的是GET请求. 如果url接口返回的是二进制流数据.则要求后端content-type 里面填写具体的返回文件类型.否则下在的文件会没有后缀名.比如exce ...
- Request和Response-学习笔记02【请求转发和request共享数据、Request_获取ServletContext、request登录案例】
Java后端 学习路线 笔记汇总表[黑马程序员] Request和Response-学习笔记01[Request_原理和继承体系.Request_获取请求数据][day01] Request和Resp ...
- 单点登录原理和java实现简单的单点登录
摘要:单点登录(SSO)的技术被越来越广泛地运用到各个领域的软件系统当中.本文从业务的角度分析了单点登录的需求和应用领域:从技术本身的角度分析了单点登录技术的内部机制和实现手段,并且给出Web-SSO ...
- Android(java)学习笔记155:中文乱码的问题处理(qq登录案例)
1. 我们在之前的笔记中LoginServlet.java中,我们Tomcat服务器回复给客户端的数据是英文的"Login Success","Login Failed& ...
- Spring Security Oauth2 单点登录案例实现和执行流程剖析
我已经试过了 教程很完美 Spring Security Oauth2 OAuth是一个关于授权的开放网络标准,在全世界得到的广泛的应用,目前是2.0的版本.OAuth2在"客户端" ...
- JDBC学习笔记02【ResultSet类详解、JDBC登录案例练习、PreparedStatement类详解】
黑马程序员-JDBC文档(腾讯微云)JDBC笔记.pdf:https://share.weiyun.com/Kxy7LmRm JDBC学习笔记01[JDBC快速入门.JDBC各个类详解.JDBC之CR ...
- Android 打造完美的侧滑菜单/侧滑View控件
概述 Android 打造完美的侧滑菜单/侧滑View控件,完全自定义实现,支持左右两个方向弹出,代码高度简洁流畅,兼容性高,控件实用方便. 详细 代码下载:http://www.demodashi. ...
最新文章
- 三天打入CV大赛决赛圈!我是如何做到的?
- R语言与数据分析(8)-获取帮助
- NYOJ--2--括号配对问题
- Timestamp、String、Date之间的转换
- arcgis-“一个或多个已经添加图层的范围与关联空间参考不一致”“Arcmap不能绘制一个或者多个图层”
- Linux配置Java,kafka,Hadoop等环境变量
- iOS 系统汉化的plist设置
- C++总结笔记(八)—— 菱形继承
- 吴恩达深度学习 —— 2.14 向量化逻辑回归的梯度输出
- spark rdd详解二(transformation与action操作)
- AHP层次分析法(附matlab程序)
- 数据库优化相关面试题
- 台式计算机 蓝牙,台式电脑的蓝牙在哪里?台式机怎么打开蓝牙?
- 关于Android studio在ubuntu中真机测试运行出现Gradle build daemon disappeared unexpectedly的一个原因及解决办法
- 解决VMware虚拟机宿主机与虚拟机通讯慢
- Linux网卡up但是没有running,eth0 up但是没有running的小问题
- 销售凭证、客户主数据
- 优化OpenSearch的搜索结果
- Windows 7系统实用进阶技巧
- Makefile详解(自己觉得重新看一次学了好多东西,红色字)