編輯:關於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 MapgetTxtFileInfo(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 map = new HashMap (); // 使用保存信息使用的##將內容分割出來 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");
最後就是需要添加權限,因為獲取外部存儲設備是有關安全的,所以需要添加相關的權限
使用這幾種方法就可以實現保存用戶名和密碼這個功能了,至於有沒有其他的方法,我想這幾種應該就夠用了
Android 高仿微信支付數字鍵盤功能
現在很多app的支付、輸入密碼功能,都已經開始使用自定義數字鍵盤,不僅更加方便、其效果著實精致。下面帶著大家學習下,如何高仿微信的數字鍵盤,可以拿來直接用在自身的項目中。
Android OpenGLES2.0(六)——構建圓錐、圓柱和球體
之前的博客中,我們繪制了三角形、正方形、圓形、立方體,今天我們將繪制圓錐、圓柱和球體。能夠繪制這些基本的常規幾何形體後,其他的常見幾何形體的繪制對於我們來說就基本沒問題了
Android簡單的學生管理系統
(1)管理系統實現的功能主要是:學生、教師的注冊登錄,和選課,以及修改學生的成績等基本簡單的功能,最主要的是實現一些Dialog的使用。界面如下:(2)主要代碼如下:(個
Android網絡框架Volley
Volley是Google I/O 2013推出的網絡通信庫,在volley推出之前我們一般會選擇比較成熟的第三方網絡通信庫,如:android-async-httpre