广播类型
1.标准广播:
broadcastReceiver同时收到广播信息
2.有序广播:
一个一个发
接受系统广播
1.动态注册broadcastReceiver 用代码注册 在当前class中写receiver类
2.静态注册broadcastReceiver 用androidManifest.xml
动态注册
1.新建一个类,继承BroadcastReceiver 重写onReceive(),当广播来时,用onReceive来处理
要创建一个IntentFilter实例 add action 来表明要接受怎么样的广播
最后registerReceiver
distoty时要unregisterReceiver()
静态注册:
添加一个receiver.class,在xml 添加action写 onreceive()
发送自定义广播
感觉和隐式intent差不多,,,都要写action
sendOrderedBroadcast有序广播
action中intent-filter android:priority设置优先级
abroadBroadcast()将方法截断
数据持久化
1.文件存储
2.sharedpreferences存储
3.数据库存储
文件存储
openFileOutput() // MODE_PRIVATE 覆盖 MODE_APPEND追加
返回FileOutputStream对象
fun save(inputText : String){
try{
val output = openFileOutput("data",Context.MODE_PRIVATE)
val writter = BufferedWriter(OutputStreamWriter(output))
write.use{
it.write(inputText)
}
}catch(e : IOException){
e.printStackTrace()
}
}
从文件中读取数据
openFileInput() //从文件读取数据
fun load() : String{
val content = StringBuilder()
try{
val input = openFileInput("data")
val reader = BufferedReader(InputStreamReader(input))
reader.use{
reader.forEachLine {
content.append(it)
}
}
}catch(e :IOException) {
e.printStackTrace()
}
return content.toString()
}
sharedPreferences存储
怎么看,都比文件方便…
1.getSharedPreferences()
2.getPreferences()
1.用sharedPreferences的edit获得SharedPreferences.Editor()
2.向sharedPreferences.editor()添加数据putboolean...
3.用apply()方法将数据提交
从SharedPreferences中读取数据
val prefs = getSharedPreferences("data",Context.MODE_PRIVATE)
val name = prefs.getString("name","")
val age = prefs.getInt("age",0)
val married = prefs.getBoolean("married",false)
Log.d("12","name is $name")
Log.d("12","age is $age")
Log.d("12","married is $married")
SQLite数据库存储
添加:db.insert(table,null,contentvalues)
更新:db.update(table,contentvalues,“name = ?”,arrayOf(“The Da Vinci Code”))
删除:db.delete()
查询:db.query()
使用事务: