Android开发中保存用户名和密码

我们不管在开发一个项目或者使用别人的项目,都有用户登录功能,为了让用户的体验效果更好,我们通常会做一个功能,叫做保存用户,这样做的目地就是为了让用户下一次再使用该程序不会重新输入用户名和密码,这里我使用3种方式来存储用户名和密码

1、通过普通 的txt文本存储

2、通过properties属性文件进行存储

3、通过SharedPreferences工具类存储

第一种:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
      * 保存用户名和密码的业务方法
      *
      * @param username
      * @param password
      * @return
      */
     public static boolean saveUserInfo(String username, String password) {
         try {
             // 使用当前项目的绝对路径
             File file = new File( "data/data/com.example.android_file_handler/info.txt" );
             // 创建输出流对象
             FileOutputStream fos = new FileOutputStream(file);
             // 向文件中写入信息
             fos.write((username + "##" + password).getBytes());
             // 关闭输出流对象
             fos.close();
             return true ;
         } catch (Exception e) {
             throw new RuntimeException();
         }
     }

这里写的路径是当前项目的绝对路径,这样做是有缺陷的,比如你将项目路径改了,这里的路径就获取就失败了,所以Android提供了通过上下文一个方法获取当前项目的路径

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static boolean saveUserInfo(Context context, String username,
             String password) {
         try {
             // 使用Android上下问获取当前项目的路径
             File file = new File(context.getFilesDir(), "userinfo.txt" );
             // 创建输出流对象
             FileOutputStream fos = new FileOutputStream(file);
             // 向文件中写入信息
             fos.write((username + "##" + password).getBytes());
             // 关闭输出流对象
             fos.close();
             return true ;
         } catch (Exception e) {
             throw new RuntimeException();
         }
     }

上面这两个方法都是存储用户名和密码,接下来是获取用户名和密码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
      * 获取普通txt文件信息
      *
      * @param context
      * @return
      */
     public static Map<string, object= "" > getTxtFileInfo(Context context) {
         try {
             // 创建FIle对象
             File file = new File(context.getFilesDir(), "userinfo.txt" );
             // 创建FileInputStream对象
             FileInputStream fis = new FileInputStream(file);
             // 创建BufferedReader对象
             BufferedReader br = new BufferedReader( new InputStreamReader(fis));
             // 获取文件中的内容
             String content = br.readLine();
             // 创建Map集合
             Map<string, object= "" > map = new HashMap<string, object= "" >();
             // 使用保存信息使用的##将内容分割出来
             String[] contents = content.split( "##" );
             // 保存到map集合中
             map.put( "username" , contents[ 0 ]);
             map.put( "password" , contents[ 1 ]);
             // 关闭流对象
             fis.close();
             br.close();
             return map;
         } catch (Exception e) {
             e.printStackTrace();
             return null ;
         }
     }</string,></string,></string,>

这里我将获取到的内容封装到Map集合中,其实使用普通的txt文本存储用户名和密码是有缺陷的,这里我是通过“##”来分割用户名和密码的,那么如果用户在密码中的字符又包含了“#”这个特殊符号,那么最后在获取时,则获取不倒原来保存的信息,所以个人认为dier中方法就可以解决这个问题

第二种:

使用属性文件保存用户名和密码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
      * 使用属性文件保存用户的信息
      *
      * @param context 上下文
      * @param username 用户名
      * @param password  密码
      * @return
      */
     public static boolean saveProUserInfo(Context context, String username,
             String password) {
         try {
             // 使用Android上下问获取当前项目的路径
             File file = new File(context.getFilesDir(), "info.properties" );
             // 创建输出流对象
             FileOutputStream fos = new FileOutputStream(file);
             // 创建属性文件对象
             Properties pro = new Properties();
             // 设置用户名或密码
             pro.setProperty( "username" , username);
             pro.setProperty( "password" , password);
             // 保存文件
             pro.store(fos, "info.properties" );
             // 关闭输出流对象
             fos.close();
             return true ;
         } catch (Exception e) {
             throw new RuntimeException();
         }
     }

读取属性文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
      * 返回属性文件对象
      *
      * @param context 上下文
      * @return
      */
     public static Properties getProObject(Context context) {
         try {
             // 创建File对象
             File file = new File(context.getFilesDir(), "info.properties" );
             // 创建FileIutputStream 对象
             FileInputStream fis = new FileInputStream(file);
             // 创建属性对象
             Properties pro = new Properties();
             // 加载文件
             pro.load(fis);
             // 关闭输入流对象
             fis.close();
             return pro;
         } catch (Exception e) {
             e.printStackTrace();
             return null ;
         }
     }


在主方法中调用即可

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 获取属性文件对象
          Properties pro=LoginService.readSDCard( this );
         // 获取用户名或密码
         if ( null != pro) {
             String username=pro.getProperty( "username" );
             String password=pro.getProperty( "password" );
             // 如果获取到的用户名或密码不为空,则设置到文本框中
             if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
                 // 设置用户名
                 etUsername.setText(username);
                 // 设置密码
                 etPassword.setText(password);
             }
         }

第三种:

使用SharedPreferences保存用户名和密码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
      * 使用SharedPreferences保存用户登录信息
      * @param context
      * @param username
      * @param password
      */
     public static void saveLoginInfo(Context context,String username,String password){
         //获取SharedPreferences对象
         SharedPreferences sharedPre=context.getSharedPreferences( "config" , context.MODE_PRIVATE);
         //获取Editor对象
         Editor editor=sharedPre.edit();
         //设置参数
         editor.putString( "username" , username);
         editor.putString( "password" , password);
         //提交
         editor.commit();
     }


在主方法中读取:

?
1
2
3
SharedPreferences sharedPre=getSharedPreferences( "config" , MODE_PRIVATE);
         String username=sharedPre.getString( "username" , "" );
         String password=sharedPre.getString( "password" , "" );


使用普通txt文件与属性文件保存都是保存到内部存储设备中,我们也可以保存的SDCard中,使用Android提供的Environment类就可以获取到外部存储设备

?
1
File file= new File(Environment.getExternalStorageDirectory(), "info.properties" );

如果要使用绝对路径

?
1
File file = new File( "/sdcard/info.properties" );

最后就是需要添加权限,因为获取外部存储设备是有关安全的,所以需要添加相关的权限

?
1
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>

使用这几种方法就可以实现保存用户名和密码这个功能了,至于有没有其他的方法,我想这几种应该就够用了

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值