Android保存用戶名和密碼


我們不管在開發一個項目或者使用別人的項目,都有用戶登錄功能,為了讓用戶的體驗效果更好,我們通常會做一個功能,叫做保存用戶,這樣做的目地就是為了讓用戶下一次再使用該程序不會重新輸入用戶名和密碼,這里我使用3種方式來存儲用戶名和密碼

1、通過普通 的txt文本存儲

2、通過properties屬性文件進行存儲

3、通過SharedPreferences工具類存儲

第一種:

/**
     * 保存用戶名和密碼的業務方法
     * 
     * @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提供了通過上下文一個方法獲取當前項目的路徑

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();
        }
    }

上面這兩個方法都是存儲用戶名和密碼,接下來是獲取用戶名和密碼

/**
     * 獲取普通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;
        }
    }

這里我將獲取到的內容封裝到Map集合中,其實使用普通的txt文本存儲用戶名和密碼是有缺陷的,這里我是通過“##”來分割用戶名和密碼的,那么如果用戶在密碼中的字符又包含了“#”這個特殊符號,那么最后在獲取時,則獲取不倒原來保存的信息,所以個人認為dier中方法就可以解決這個問題

第二種:

使用屬性文件保存用戶名和密碼

/**
     * 使用屬性文件保存用戶的信息
     * 
     * @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();
        }
    }

讀取屬性文件

/**
     * 返回屬性文件對象
     * 
     * @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;
        }
    }

在主方法中調用即可

// 獲取屬性文件對象
         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保存用戶名和密碼

/**
     * 使用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();
    }

在主方法中讀取:

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

使用普通txt文件與屬性文件保存都是保存到內部存儲設備中,我們也可以保存的SDCard中,使用Android提供的Environment類就可以獲取到外部存儲設備

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

如果要使用絕對路徑

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

最后就是需要添加權限,因為獲取外部存儲設備是有關安全的,所以需要添加相關的權限

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

使用這幾種方法就可以實現保存用戶名和密碼這個功能了,至於有沒有其他的方法,我想這幾種應該就夠用了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM