android(5)(模拟QQ登录,文件存储,SD卡存储,SharedPreferences存储)
2015-12-04 11:41
453 查看
模拟QQ登录(记录账号和密码): 1.文件存储到文件中: /** * 文件存储 * @author Administrator * */ public class Utils { /** * 保存用户信息到文件 * @param number * @param password * @return true 成功 */ public static boolean saveUserInfo(String number, String password) { try { String path = "/data/data/com.itheima.qqlogin/itheima.txt"; FileOutputStream fos = new FileOutputStream(path); // 307966990##123123 String data = number + "##" + password; fos.write(data.getBytes()); fos.flush(); fos.close(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } /** * 保存用户信息 * @param number * @param password * @return true 成功 */ public static boolean saveUserInfo(Context context, String number, String password) { try { // String path = "/data/data/com.itheima.qqlogin/itheima.txt"; // File filesDir = context.getFilesDir();//存储用户信息到file文件中 File filesDir = context.getCacheDir();//存储用户信息到缓存 File f = new File(filesDir, "itheima.txt"); FileOutputStream fos = new FileOutputStream(f); // 307966990##123123 String data = number + "##" + password; fos.write(data.getBytes()); fos.flush(); fos.close(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } /** * 返回用户信息(从文件中读取数据) * @return */ public static Map<String, String> getUserInfo() { try { String path = "/data/data/com.itheima.qqlogin/itheima.txt"; FileInputStream fis = new FileInputStream(path); // 字符流对象(字符缓冲流是高效流) BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); // 307966990##123123 String text = reader.readLine(); if(!TextUtils.isEmpty(text)) { String[] split = text.split("##"); Map<String, String> userInfoMap = new HashMap<String, String>(); userInfoMap.put("number", split[0]); userInfoMap.put("password", split[1]); return userInfoMap; } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 返回用户信息 * @return */ public static Map<String, String> getUserInfo(Context context) { try { // String path = "/data/data/com.itheima28.qqlogin/itheima28.txt"; // File filesDir = context.getFilesDir(); File filesDir = context.getCacheDir(); File f = new File(filesDir, "itheima.txt"); FileInputStream fis = new FileInputStream(f); // 字符流对象 BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); // 307966990##123123 String text = reader.readLine(); if(!TextUtils.isEmpty(text)) { String[] split = text.split("##"); Map<String, String> userInfoMap = new HashMap<String, String>(); userInfoMap.put("number", split[0]); userInfoMap.put("password", split[1]); return userInfoMap; } } catch (Exception e) { e.printStackTrace(); } return null; } 2.文件存储到SD卡中: /** * 保存用户信息到sd卡 * @param number * @param password * @return true 成功 */ public static boolean saveUserInfo(Context context, String number, String password) { try { // 判断当前的手机是否有sd卡 String state = Environment.getExternalStorageState(); if(!Environment.MEDIA_MOUNTED.equals(state)) { // 已经挂载了sd卡 return false; } File sdCardFile = Environment.getExternalStorageDirectory(); File file = new File(sdCardFile, "itheima.txt"); FileOutputStream fos = new FileOutputStream(file); String data = number + "##" + password; fos.write(data.getBytes()); fos.flush(); fos.close(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } /** * 到sd卡获取用户信息 * @return */ public static Map<String, String> getUserInfo(Context context) { try { // 判断当前的手机是否有sd卡 String state = Environment.getExternalStorageState(); if(!Environment.MEDIA_MOUNTED.equals(state)) { // 已经挂载了sd卡 return null; } File sdCardFile = Environment.getExternalStorageDirectory(); File file = new File(sdCardFile, "itheima.txt"); //字符缓冲流(高效流)从SD卡中读文件 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String text = br.readLine(); br.close(); if(!TextUtils.isEmpty(text)) { Map<String, String> userInfoMap = new HashMap<String, String>(); String[] split = text.split("##"); userInfoMap.put("number", split[0]); userInfoMap.put("password", split[1]); return userInfoMap; } } catch (Exception e) { e.printStackTrace(); } return null; } 3.文件存储:用sharedpreferences保存用户信息: /** * 用sharedpreferences保存用户信息 * @param number * @param password * @return true 成功 */ public static boolean saveUserInfo(Context context, String number, String password) { try { // /data/data/包名/shared_prefs/itheima SharedPreferences sp = context.getSharedPreferences("itheima", Context.MODE_PRIVATE); // 获得一个编辑对象 Editor edit = sp.edit(); // 存数据 edit.putString("number", number); edit.putString("password", password); // 提交, 数据真正存储起来了. edit.commit(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } /** * 到sd卡获取用户信息(存储到一个集合中,然后再返回) * @return */ public static Map<String, String> getUserInfo(Context context) { SharedPreferences sp = context.getSharedPreferences("itheima", Context.MODE_PRIVATE); String number = sp.getString("number", null); String password = sp.getString("password", null); if(!TextUtils.isEmpty(number) && !TextUtils.isEmpty(password)) { Map<String, String> userInfoMap = new HashMap<String, String>(); userInfoMap.put("number", number); userInfoMap.put("password", password); return userInfoMap; } return null; } 4.实现登录的业务逻辑: public class MainActivity extends Activity implements OnClickListener { private static final String TAG = "MainActivity"; private EditText etNumber; private EditText etPassword; private CheckBox cbRemerberPWD; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etNumber = (EditText) findViewById(R.id.et_number); etPassword = (EditText) findViewById(R.id.et_password); cbRemerberPWD = (CheckBox) findViewById(R.id.cb_remerber_pwd); Button btnLogin = (Button) findViewById(R.id.btn_login); btnLogin.setOnClickListener(this); // 回显数据 Map<String, String> userInfoMap = UtilsOfSharedPreferences.getUserInfo(this); if(userInfoMap != null) { etNumber.setText(userInfoMap.get("number")); etPassword.setText(userInfoMap.get("password")); } } @Override public void onClick(View v) { // 执行登录的操作 // 1. 取出号码和密码 String number = etNumber.getText().toString(); String password = etPassword.getText().toString(); if(TextUtils.isEmpty(number) || TextUtils.isEmpty(password)) { // 弹出吐司 Toast.makeText(this, "请正确输入", Toast.LENGTH_SHORT).show(); return; } // 2. 判断记住密码是否被选中, 如果被选中, 存起来 if(cbRemerberPWD.isChecked()) { // 当前需要记住密码 Log.i(TAG, "记住密码: " + number + ", " + password); boolean isSuccess = UtilsOfSharedPreferences.saveUserInfo(this, number, password); if(isSuccess) { Toast.makeText(this, "保存成功", 0).show(); } else { Toast.makeText(this, "保存失败", 0).show(); } } // 3. 登陆成功 Toast.makeText(this, "登录成功", 0).show(); } 5.文件存储: this.getFilesDir();// /data/data/包名/files. this.getCacheDir();// /data/data/包名/cache. SharedPreferences存储路径: /data/data/包名/shared_prefs/. SD卡: // /mnt/sdcard.
相关文章推荐
- Android笔记——Button点击事件几种写法
- Menu模拟键与android:targetSdkVersion
- 创建简单的Android Material DesignDemo
- android:versionCode和android:versionName 用途
- android:exported 属性详解
- Android 开发必备知识:我和 Gradle 有个约会
- Android编程——自定义View类onSaveInstanceState与onRestoreInstanceState使用方法
- android学习第一站
- ZZ: Service与Android系统设计 (分析的不错)
- Android编译过程详解(一)
- Android 开发必备知识:我和 Gradle 有个约会
- 【Service 1】Android Remote Service - AIDL
- Android源码开发之控制开关机铃声
- Android编译过程详解(一)
- android 统计图表MPAndroidChart
- Android的材料设计兼容库(Design Support Library)
- Android.mk 文件语法详解
- android学习笔记(六):在项目中查看v4包源码
- Android四大组件详解
- 野人学Android第二弹——图片上传到服务器的基础原理