Android 开发(13)数据存储技术
程序员文章站
2024-03-20 11:24:34
...
一、shared preferences存储
1、SP存取数据
写数据
读数据
2、QQ自动登录实现
public class MainActivity extends AppCompatActivity {
private EditText editText1, editText2;
private Button button;
private String username = "stan", password = "stan123";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.et1);
editText2 = (EditText) findViewById(R.id.et2);
button = (Button) findViewById(R.id.btn);
final SharedPreferences sp = getSharedPreferences("stan", MODE_PRIVATE);
String user = sp.getString("user", null);
String pass = sp.getString("pass", null);
if (user != null && pass != null) {
if (user.equals(username) && pass.equals(password)) {
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
startActivity(intent);
}
} else {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username_in = editText1.getText().toString();
String password_in = editText2.getText().toString();
if (!(username_in == null) && !(password_in == null)) {
if (username_in.equals(username) && password_in.equals(password)) {
SharedPreferences.Editor editor = sp.edit();
editor.putString("user", username_in);
editor.putString("pass", password_in);
editor.commit();
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "用户名或者密码错误", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "用户名或者密码为空", Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
sharedPreferences 最终将数据保存在一个 xml 文件中,这个文件所在的位置是
data>data>包名>shared_prefs
二、文件存储
- 这里的外部存储和内部存储不是指内置存储和SD的卡的差异
1、内部存储
简易备忘录的实现
public class MainActivity extends AppCompatActivity {
private ImageButton btn_cancel, btn_save;
private EditText editText;
private FileInputStream fis;
private FileOutputStream fos;
private byte[] buffer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_cancel = (ImageButton) findViewById(R.id.cancel);
btn_save = (ImageButton) findViewById(R.id.save);
editText = (EditText) findViewById(R.id.et);
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
fos = openFileOutput("inner", MODE_PRIVATE);
String text = editText.getText().toString();
fos.write(text.getBytes());
fos.flush();
Toast.makeText(MainActivity.this, "文件已经保存", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
//open app to open file
try {
fis = openFileInput("inner");
buffer = new byte[fis.available()];
fis.read(buffer);
editText.setText(new String(buffer));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
2、外部存储
- 简单来说我们的手机连接电脑,在电脑上能够查看的部分就是外部存储
- 上面的备忘录实例将获取输入和输出流的部分改为直接新建一个相应的FOS或者FIS对象并指定相应的File文件就可以了
关键代码
- 记得要在 manifest 里面开启权限
final File file = new File(Environment.getExternalStorageDirectory(),"stan.txt");
fos = new FileOutputStream(file);
fis = new FileInputStream(file);
三、数据库存储
四、数据共享
推荐阅读
-
Android 开发(13)数据存储技术
-
Android开发之数据保存技术(一)
-
Android APP 开发技术阶段总结(三)
-
Android硬件开发之——蓝牙技术
-
i-jetty——Android开发数据模拟 博客分类: Android android data silumationAndroid数据模拟i-jetty
-
数据库事务隔离级别 博客分类: 技术开发 数据库事务隔离级别脏读不可重复读幻读
-
Android数据存储之SharedPreferences详细总结
-
Android数据的四种存储方式之 —— SharePreferences
-
android 数据库存储之GreenDao的使用
-
Android数据存储