对于用户来说,一个新的APP就像一个橡皮泥,需要根据自己的需求,个性化定制为适合自己的样子,而用户设置之后,要保证下次打开的时候,仍然是设置后的状态,就需要用到数据存储技术。
常见的数据存储技术:
- SharePreferences存储
- 文件存储
- 数据库存储
- Content Provider实现数据共享
- SharePreferences存储
它适合用来存储少量数据。因此,类似配置信息和登录信息比较适合,大一点的文件适合采用其它的方式。位于手机/data/data/<应用程序包名>/shared_prefs下。
示例:下面代码,实现用户选中记住密码之后,不需要登录的机制。如果没有选中记住密码,下次仍然需要登录。
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.Toast
class LoginActivity : Activity() {
private val name:String = "123456"//后台获取到的账号
private val password_true:String = "123456"//后台获取到的密码
private var username:String = ""
private var pwd:String = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.login)//登录界面
//登录信息存储
val count:EditText = findViewById(R.id.count)//账号
val password:EditText = findViewById(R.id.password)//密码
val remember:CheckBox = findViewById(R.id.remember_checkbox)//记住密码
val login:Button = findViewById(R.id.login_button)//登入
val sp:SharedPreferences = getSharedPreferences("info", Context.MODE_PRIVATE)
val editor:SharedPreferences.Editor = sp.edit()
if(sp.getString("username", username) != null && sp.getString("password", pwd) != null){
if(sp.getString("username", username).equals(name) && sp.getString("password", pwd).equals(password_true)){
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
this.finish()
}
}
//没有缓存才会进入这里
login.setOnClickListener {
username = count.text.toString()
pwd = password.text.toString()
if(username.equals(name) && pwd.equals(password_true)){
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show()
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
if(remember.isChecked){
editor.putString("username", username)
editor.putString("password", pwd)
editor.apply()
}
this.finish()
}else{
Toast.makeText(this, "密码或者账号错误", Toast.LENGTH_SHORT).show()
}
}
}
}
- 文件存储
var fos:FileOutputStream? = null
try {
//ps:这个memo在手机上需要正确设置根目录,否则找不到文件
fos = openFileOutput("memo", Context.MODE_PRIVATE)
fos.write("测试".toByteArray())
fos.flush()
}catch (e:IOException){
e.printStackTrace()
}finally {
if(fos != null){
try {
fos.close()
}catch (e:IOException){
e.printStackTrace()
}
}
}
var fis:FileInputStream? = null
var buffer:ByteArray? = null
try {
fis = openFileInput("memo")
buffer = ByteArray(fis.available())
fis.read(buffer)
}catch (e:IOException){
e.printStackTrace()
}catch ( e: FileNotFoundException){
e.printStackTrace()
}finally {
if(fis != null){
try {
fis.close()
var data:String = buffer.toString()
println(data)
}catch (e:IOException){
e.printStackTrace()
}
}
}
- 数据库存储
创建数据库:
static SQLiteDatabase openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory)
添加数据:
public long insert(String table, String nullColumnHack, ContentValues values)
更新操作:
public in update(String table, ContentValues values, String whereClause, String[] whereArgs)
删除操作:
public int delete(String table, String whereClause, String[] whereArgs)
查询操作:
public Cursor query(boolean distinct, String table, String[] columns,
String selection, String[] selectionArgs, String groupBy ,
String having, String orderBy, String limit) - Content Provider实现数据共享
四部分组成了查询的URI:
content:(A)//com.mingrisoft.employeeprovider(B)/person©/001(D)
A部分:标准前缀
B部分:URI权限部分
C部分:Content Provider的路径部分。
D部分:被请求的特定记录的ID。
示例比较长,写在下一个博客。
使用实例,使用Kotlin实现