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

Android核心基础——S02E06_保存数据到手机内存

2014-08-27 01:44 435 查看
数据存储与访问

很多时候我们的软件需要对处理后的数据进行存储或再次访问。Android为数据存储提供了如下几种方式:

l文件:流操作

lSharedPreferences(参数):维护的是XML文件。

lSQLite数据库

lContentprovider内容提供者

l网络

实现QQ登陆界面记住密码的功能。

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<EditText
android:id="@+id/et_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入QQ号" />

<EditText
android:id="@+id/et_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword" />

<CheckBox
android:id="@+id/cb_remerber_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="记住密码" />

<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录" />

</LinearLayout>



MainActivity.java文件:

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 = 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();
}
}


Utils.java文件:

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.text.TextUtils;

public class Utils {

/**
* 保存用户信息
* @param number
* @param password
* @return true 成功
*/
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;
}

/**
* 保存用户信息
* @param number
* @param password
* @return true 成功
*/
public static boolean saveUserInfo(Context context, String number, String password) {

try {
//			String path = "/data/data/com.itheima28.qqlogin/itheima28.txt";

//			File filesDir = context.getFilesDir();
File filesDir = context.getCacheDir();

File f = new File(filesDir, "itheima28.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.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;
}

/**
* 返回用户信息
* @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, "itheima28.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;
}
}


注:这是将信息保存在手机内部,因此可以不用获得手机权限。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐