您的位置:首页 > 移动开发 > Android开发

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: