【Android-基础】
文章目录
前言
安卓的四种存储数据的方式为:
1、SharedPreferences
2、文件存储(内部+外部):通过IO流读取文件
3、Sqlite
4、跨应用数据共享ContentProvide
一、SharedPreferences:最简单的
这种方式会将数据以xml文件的形式存储
存储步骤:
①取出SharedPreferences:
方式1:getSharedPreferences(文件名,权限);
方式2:getPreferences(权限);
前者适合多个SharedPreferences文件,后者如果程序只有一个SharedPreferences,就用后者。
权限:MODE_PRIVATE代表这个文件,只能被本程序使用。
MODE_MULTI_PROCESS代表这个文件可以跨应用使用。
②取出 SharedPreferences.Editor对象,调用SharedPreferences的edit方法。
③用 SharedPreferences.Editor对象存数据,用其putString、putInt等方法。
④提交存储,用SharedPreferences.Editor对象的commit方法。
取出步骤:
①取出SharedPreferences。
②用SharedPreferences的getString、getInt方法取出
代码:
public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init() {
SharedPreferences sp = getSharedPreferences("test",MODE_MULTI_PROCESS);
//存
SharedPreferences.Editor editor = sp.edit();
editor.putString("data","这是第一条数据");
editor.commit();
//取
String dataStr = sp.getString("data","");//data没有值,默认取出“”
System.out.println(dataStr);
}
}
二、文件存取
I/O其实就是input/output,前者叫做输入流,后者叫做输出流,这个可以简单的字面理解,输入流,就是文件流输入进app。(一)文件存储之内部存储
特点:
1、这个会将数据存在files文件下
2、默认被创建这个文件的app使用
3、app卸载,这个文件也删除
4、内部存储满了,手机就会不能用,所以尽量少用这个。
代码:
public class NeibuCunchu {
public Context context;
public NeibuCunchu(Context context) {
this.context = context;
}
/**
* 存储数据方法
*/
public void saveData(String savaDataString){
FileOutputStream fos = null;
try {
//fileName是存储数据的文件名,后面是权限,权限参考SharedPreferences的权限
fos = context.openFileOutput("fileName",Context.MODE_PRIVATE);
//数据写入文件输出流
fos.write(savaDataString.getBytes());
//清除缓存
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 获取数据
*/
public void getData(){
FileInputStream fis = null;
try {
fis = context.openFileInput("fileName");
byte[] bytes = null;
//初始化并用available方法设置字节数组大小
bytes = new byte[fis.available()];
//数据存入文件输入流
fis.read(bytes);
//将文件信息读出打印
System.out.println(new String(bytes));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(二)文件存储之外部存储
步骤:
1、获取外部存储目录:
2、读写外部存储文件:FileInputStream和FileOutputStream
3、在AndroidManifest.xml设置读写权限。
代码:
public class WaibuCunchu {
public File file;
public WaibuCunchu() {
//根目录创建data.txt文件作为数据存储文件
this.file = new File(Environment.getExternalStorageDirectory(),"data.txt");
}
/**
* 存储数据方法
*/
public void saveData(String savaDataString){
FileOutputStream fos = null;
try {
//fos用FileOutputStream初始化
fos = new FileOutputStream(file);
//数据写入文件输出流
fos.write(savaDataString.getBytes());
//清除缓存
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 获取数据
*/
public void getData(){
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
byte[] bytes = null;
//初始化并用available方法设置字节数组大小
bytes = new byte[fis.available()];
//数据存入文件输入流
fis.read(bytes);
//将文件信息读出打印
System.out.println(new String(bytes));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
在AndroidManifest.xml文件中设置权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
三、Sqlite存储
1、优势:占用资源少、运行效率高、安全、可移植性强。
2、步骤:
(1)创库
①用openOrCreateDatabase方法
②继承SQLiteOpenHelper类
(2)操作
增删改查对应insert()\delete()\update()\query()
3、代码:
用继承SQLiteOpenHelper类方式
MyDbHelper文件:
public class MyDbHelper extends SQLiteOpenHelper {
public MyDbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
//工厂为null
super(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//执行建表语句
db.execSQL("create table usertable (name int, pwd int)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//更新数据库处理
}
}
使用的时候:
public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init() {
//数据库存数据
//参数1:上下文;参数2:库名;参数3:工厂;参数4:数据库版本
MyDbHelper db = new MyDbHelper(this,"kuMing",null,1);
ContentValues values = new ContentValues();
values.put("name",1);
values.put("pwd",1);
//参数1:表名;参数2:哪个字段可以为null,null代表都不为null;参数3:要插入的数据
db.getReadableDatabase().insert("usertable",null,values);
//数据库取数据
//参数1:查的表名;参数2:查哪一列数据,null代表查全部列数据;参数3:查询条件;参数4:查询条件的参数;后面的参数不做赘述
Cursor cursor = db.getReadableDatabase().query("usertable",null,"name=?",new String[]{"1"},null,null,null);
ArrayList<Map<String,String>> list = new ArrayList<>();
//取下一条数据
while (cursor.moveToNext()){
Map<String,String> map = new HashMap<>();
//取第0列赋值给name
map.put("name",cursor.getString(0));
map.put("name",cursor.getString(1));
list.add(map);
}
//展示查出的数据
for (Map<String, String> map : list) {
System.out.println(map.get("name")+"---"+map.get("pwd"));
}
}
}
四、跨应用数据共享ContentProvide(不常用)
1、知识点:
数据模型:基于数据模型的简单表格为ContentProvide提供数据
URI:统一资源标识符
2、步骤:
①继承ContentProvider
②声明权限,在AndroidManifest.xml用标签
③增删改查操作,用ContentProvide的insert()\delete()\update()\query()