編輯:關於 Android 程式設計
登入功能幾乎是每個 App 的標配,而「記住我」則是使用者體驗的關鍵——下次打開不必再敲一次帳密。Android 上常見的實素有/plain 文字檔、Properties 屬性檔、以及 SharedPreferences 三種,各自的適用情境與風險並不相同。
最直觀的做法是把帳號與密碼用某個分隔字串串接後寫入檔案:
public static boolean saveUserInfo(Context context, String username, String password) {
try {
// 用 Context 取得 App 私有目錄,不要寫死絕對路徑
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();
}
}
讀回時用 BufferedReader 逐行取出,再以 ## 切分,分別塞進 Map 供呼叫端使用:
public static Map<String, Object> getTxtFileInfo(Context context) {
try {
File file = new File(context.getFilesDir(), "userinfo.txt");
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String content = br.readLine();
Map<String, Object> map = new HashMap<String, Object>();
String[] contents = content.split("##");
map.put("username", contents[0]);
map.put("password", contents[1]);
fis.close();
br.close();
return map;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
寫死路徑 new File("data/data/com.example.xxx/info.txt") 是很多教學會犯的錯——一旦改了套件名稱或換裝置,路徑就失效。正確作法永遠是透過 context.getFilesDir() 取得私有目錄。
而這個方案真正的致命缺陷在於分隔符號:如果使用者密碼本身含有 #,切分出來的第二段就會是殘缺的密碼。這正是下一種方案存在的理由。
Java 標準的 Properties 以 key-value 成對存放,不必自己設計分隔規則,也不會有跳脫字元的困擾:
public static boolean saveProUserInfo(Context context, String username, String password) {
try {
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();
}
}
public static Properties getProObject(Context context) {
try {
File file = new File(context.getFilesDir(), "info.properties");
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.getProObject(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);
}
}
這是 Android 官方為「少量鍵值設定」設計的元件,底層其實就是 XML 檔,但由 framework 負責讀寫與快取,API 最簡潔:
public static void saveLoginInfo(Context context, String username, String password) {
SharedPreferences sharedPre = context.getSharedPreferences("config", context.MODE_PRIVATE);
SharedPreferences.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", "");
前兩種檔案也可以改放到 SD 卡,透過 Environment 取得外部儲存路徑:
File file = new File(Environment.getExternalStorageDirectory(), "info.properties");
或使用絕對路徑:
File file = new File("/sdcard/info.properties");
並在 AndroidManifest.xml 加上權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
以上三種方式都是明文儲存。任何取得裝置 root 權限、或透過 ADB backup 的第三方,都能直接讀出密碼。實務上的建議是:
getFilesDir() 或 MODE_PRIVATE 的 SharedPreferences(App 私有、其他 App 無法存取),而不要寫到外部儲存空間。
一步集成側滑刪除菜單:SwipeDelMenuLayout 使用與原理
專案地址:https://github.com/mcxtzhang/SwipeDelMenuLayout (覺得不錯的話,歡迎隨手點個 star。) 先講最重要的一句話
Chromium for Android 硬體渲染流程摘要
Render 進程中的網頁樹狀結構在 Chromium for Android 中,webkit 引擎會為網頁內容建立 DOM tree、Render tree 與 Re
Android ContentProvider 啟動過程原始碼分析:跨進程的完整握手流程
本文參考〈Android 應用程序組件 Content Provider 的啟動過程源代碼分析〉與《Android 系統源代碼情景分析》(作者:羅升陽),把 Conten
Android 5.1 Vold 增加 NTFS 存儲支持的移植思路
在 Android 原生存儲體系中,vold 負責管理可移動存儲與掛載流程。原生實現通常更偏向 FAT 等常見格式,而機頂盒、電視盒子等設備常需要支持 NTFS U 盤。