1.模拟QQ登陆,实现数据保存到内存中
package com.itheima28.qqlogin; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; import com.itheima28.qqlogin.utils.Utils; import com.itheima28.qqlogin.utils.UtilsOfSharedPreferences; 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 = Utils.getUserInfo(); 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 = Utils.saveUserInfo(number, password); if(isSuccess) { Toast.makeText(this, "保存成功", 0).show(); } else { Toast.makeText(this, "保存失败", 0).show(); } } // 3. 登陆成功 Toast.makeText(this, "登录成功", 0).show(); } }
public static boolean saveUserInfo(String number, String password) { try { String path = "/data/data/com.itheima28.qqlogin/itheima28.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; }
/** * 返回用户信息 * @return */ public static Map<String, String> getUserInfo() { try { String path = "/data/data/com.itheima28.qqlogin/itheima28.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; }
2.实现数据保存到SD卡
package com.itheima28.qqlogin.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.os.Environment; import android.text.TextUtils; public class UtilsOfSDCard { /** * 保存用户信息到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, "itheima28.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, "itheima28.txt"); 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; } }