【Android】安卓中的存盤
1、存盤在App內部
最簡單的一種,在嘗試程序中發現,手機中很多檔案夾都沒有權限讀寫,我們可以將我們需要寫的檔案存放到App中的files檔案夾中,當然我們有權限在整個App中讀寫檔案

可以通過API獲取一個file物件,這里的this就是MainActivity類
// 獲取當前包下的files路徑 /data/data/top.woodwhale.qqlogin/files
File filesDir = this.getFilesDir();
之后就可以通過檔案輸出流寫入檔案:
File filesFile = new File(filesDir,"info.txt");
boolean flag = (filesFile.exists() || filesFile.createNewFile());
FileOutputStream fos = new FileOutputStream(file);
fos.write((ac+"***"+pwd).getBytes(StandardCharsets.UTF_8));
fos.close();
寫入成功:

當然,我們既然在這個App中都有權限,那么所有目錄都可以寫:
// 寫入到自己有權限寫的地方
File file = new File("/data/data/top.woodwhale.qqlogin/info.txt");
2、SD卡外部存盤
雖然現在很多的手機都不支持SD卡了,但是仍然有平板使用,
直接放出一個Activity類,其中呼叫了nvironment.getExternalStorageDirectory();方法類獲取一個sd卡file物件,使用Formatter.formatFileSize(this,externalStorageDirectory.getFreeSpace()));Formatter類中的轉化,將long型別轉化為大小型別,同時呼叫sd卡file物件的getFreeSpace()方法,獲取卡中剩余的空間,之后就是寫入externalStorageDirectory.getPath()卡中的路徑
public class SdcardActivity extends Activity {
private Button btn;
public static String TAG = "SdcardActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sc_card_rw);
btn = this.findViewById(R.id.bt_sdw);
btn.setOnClickListener(view -> {
File externalStorageDirectory = Environment.getExternalStorageDirectory();
Log.d(TAG, "sd卡路徑是:"+externalStorageDirectory.getPath());
Log.d(TAG,"sd卡剩余空間是"+ Formatter.formatFileSize(this,externalStorageDirectory.getFreeSpace()));
File file = new File(externalStorageDirectory,"love.txt");
try {
boolean flag = file.exists() || file.createNewFile();
if (flag) {
FileOutputStream fos = new FileOutputStream(file);
fos.write("woodwhale love sheepbotany".getBytes(StandardCharsets.UTF_8));
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
但是,在此之前,我們需要一個SD卡的讀寫權限,我們在AndrodiManifest.xml中配置下面的ses-permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
最終,在我們的sd卡中看到如下結果,證明寫入成功:

3、SharedPreferences存盤
SharedPreferences是android下的一個類,功能就是記錄偏好設定,形成一個xml檔案
我們可以用SharedPreferences來存盤一些資訊,
例如常見的這種:

我們勾選之后,再次打開app仍然處于勾選狀態,
那么這種情況如何實作呢?
通過xml生成上面的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_centerVertical="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="未知來源"
android:textColor="@color/teal_200"
android:layout_marginLeft="10dp"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="運行安裝未知來源的應用"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textSize="18sp"/>
</LinearLayout>
<Switch
android:id="@+id/sw_source"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="30dp"
android:layout_marginRight="10dp" />
</RelativeLayout>
我們把Switch這個選擇框在activity類中賦予一個變數,給他加上一個OnCheckedChangeListener,再使用SharedPreferences來進行設定偏好,整體代碼如下
package top.woodwhale.qqlogin;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;
import androidx.annotation.Nullable;
public class PreferenceDemoActivity extends Activity {
private Switch sw;
public static String TAG = "PreferenceDemoActivity";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pre_demo);
sw = (Switch) this.findViewById(R.id.sw_source);
SharedPreferences settingInfo = this.getSharedPreferences("settingInfo", MODE_PRIVATE);
SharedPreferences.Editor edit = settingInfo.edit();
sw.setOnCheckedChangeListener(new MyListener(edit));
boolean state = settingInfo.getBoolean("state", true);
Log.d(TAG,"STATE=="+ state);
sw.setChecked(state);
}
}
// 改變狀態的監聽器
class MyListener implements CompoundButton.OnCheckedChangeListener {
SharedPreferences.Editor editor;
public MyListener(SharedPreferences.Editor editor) {
this.editor = editor;
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Log.d(PreferenceDemoActivity.TAG,"current state : "+ b);
editor.putBoolean("state",b); // 要保存的資料型別
editor.commit(); // 保存資料
}
}
其中,editor的功能是保存資料

其次,為了每次打開App都可以看到我們的配置,通過讀取偏好組態檔,設定switch框的勾選

這樣就可以同步偏好設定的勾選啦!
最后我們可以在手機內部看到我們寫入的偏好設定xml檔案了,這樣也屬于存盤在App內部


4、使用SQLite資料庫存盤
Android設備自帶SQLite資料庫,如果掌握過mysql,那么SQLite非常容易上手,且不說提供了非常簡便的API,就算是自己寫也比JDBC簡單!
首先我們不適用提供的API來實作一次增刪改查!
4.1 自己完成一個BaseDao類
BaseDao類本來是用來連接資料庫等基礎的,具體的增刪改查應該在service層實作,但為了這里測驗,我們將crud的方法寫入到BaseDao類中封裝起來,具體架構如下:

首先是Constants類,是常量類,其中有我們的資料庫名、版本號、表名
public class Constants {
public static final String DATABASE_NAME = "woodwhale.db";
public static final int VERSION_CODE = 1;
public static final String TABLE_NAME = "user";
}
其次是DatabaseHelper類,繼承SQLiteOpenHelper類,用來開啟資料庫,其中的onCreate方法是資料庫創建時的回呼,onUpgrade方法時升級資料時的回呼,我們在Constans類中寫了一個版本號,爸爸那邊每次升級可以加入新的功能,可以寫在onUpgrade方法中,通過switch實作,不過需要注意,升級只能讓版本號上升,不能降級,否則會報錯!
package top.woodwhale.qqlogin.SQLite.utils;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
public static String TAG = "DatabaseHelper";
/**
* @param context 背景關系
*/
public DatabaseHelper( Context context) {
super(context, Constants.DATABASE_NAME, null, Constants.VERSION_CODE);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
// 創建時的回呼
Log.d(TAG, "創建資料庫");
String sql = "create table " + Constants.TABLE_NAME + " (id integer,name varchar,age integer)";
sqLiteDatabase.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
// 升級資料庫的回呼
Log.d(TAG, "升級資料庫!");
String sql = null;
switch (i) {
case 1:
sql = "alter table "+ Constants.TABLE_NAME + " add phone integer";
sqLiteDatabase.execSQL(sql);
break;
case 2:
sql = "alter table "+ Constants.TABLE_NAME + " add address varchar";
sqLiteDatabase.execSQL(sql);
break;
}
}
}
最后就是我們封裝好的資料庫BaseDao類,通過陳述句實作了增刪改查
package top.woodwhale.qqlogin.SQLite.dao;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import top.woodwhale.qqlogin.SQLite.utils.Constants;
import top.woodwhale.qqlogin.SQLite.utils.DatabaseHelper;
// BaseDao類
public class BaseDao {
private final DatabaseHelper dbh;
private SQLiteDatabase db;
public static String TAG = "BaseDao";
public BaseDao(Context context) {
dbh = new DatabaseHelper(context);
}
// 增
public void add(int id, String name, int age) {
db = dbh.getWritableDatabase();
String sql = "insert into " + Constants.TABLE_NAME + "(id,name,age) values(?,?,?)";
Object[] params = new Object[]{id,name,age};
db.execSQL(sql,params);
db.close();
}
// 刪
public void free(int id) {
db = dbh.getWritableDatabase();
String sql = "delete from " + Constants.TABLE_NAME + " where id=?";
Object[] params = new Object[]{id};
db.execSQL(sql,params);
db.close();
}
// 改
public void edit(int id, int age) {
db = dbh.getWritableDatabase();
String sql = "update " + Constants.TABLE_NAME +" set age = ? where id = ?";
Object[] params = new Object[]{age,id};
db.execSQL(sql,params);
db.close();
}
// 查
@SuppressLint("Range")
public void show(int id) {
db = dbh.getReadableDatabase();
String sql = "select * from " + Constants.TABLE_NAME +" where id = ?";
String[] params = new String[]{String.valueOf(id)};
Cursor cursor = db.rawQuery(sql, params);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
Log.d(TAG,"name == "+name);
int age = cursor.getInt(cursor.getColumnIndex("age"));
Log.d(TAG,"age == "+age);
}
cursor.close();
db.close();
}
}
接著我們在AndroidTest包下進行測驗

package top.woodwhale.qqlogin;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
import top.woodwhale.qqlogin.SQLite.dao.BaseDao;
import top.woodwhale.qqlogin.SQLite.utils.DatabaseHelper;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
public static final String TAG = "ExampleInstrumentedTest";
public static final Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();;
public static final BaseDao dao = new BaseDao(appContext);;
@Test
public void useAppContext() {
// Context of the app under test.
assertEquals("top.woodwhale.qqlogin", appContext.getPackageName());
}
@Test
public void testCreate() {
DatabaseHelper dbh = new DatabaseHelper(appContext);
SQLiteDatabase writableDatabase = dbh.getWritableDatabase();
Log.d(TAG, writableDatabase.getPath());
}
@Test
public void testAdd() {
dao.add(1,"woodwhale",19);
dao.add(2,"sheepbotany",21);
}
@Test
public void testFree() {
dao.free(1);
}
@Test
public void testEdit() {
dao.edit(1,3);
}
@Test
public void testShow() {
dao.show(1);
}
}
增刪改查都成功,成功就如圖所示:

由于只有查詢有log回顯,在logcat中之后show方法出現了log

4.2 使用Google寫的API處理
那么使用Google寫好的增刪改查api可以避免我們sql陳述句的格式問題和語法錯誤
經過測驗,如下代碼沒有問題(在BaseDao類中)
// 使用API的添加
public void addByAPI(int id, String name, int age) {
ContentValues contentValues = new ContentValues();
contentValues.put("id",id);
contentValues.put("name",name);
contentValues.put("age",age);
db = dbh.getWritableDatabase();
db.insert(Constants.TABLE_NAME,null,contentValues);
db.close();
}
// 洗掉
public void freeByAPI(int id) {
db = dbh.getWritableDatabase();
db.delete(Constants.TABLE_NAME,"id = ?",new String[]{String.valueOf(id)});
db.close();
Log.d(TAG,"API洗掉成功!");
}
// 修改
public void editByAPI(int id, String name, Integer age) {
ContentValues contentValues = new ContentValues();
if (name != null) {
contentValues.put("name",name);
}
if (age != null) {
contentValues.put("age",age);
}
db = dbh.getWritableDatabase();
db.update(Constants.TABLE_NAME,contentValues,"id = ?", new String[]{String.valueOf(id)});
db.close();
}
// 查詢
public void showByAPI(int id) {
db = dbh.getReadableDatabase();
Cursor cursor = db.query(false, Constants.TABLE_NAME, new String[]{"id", "name", "age"}, "id = ?", new String[]{String.valueOf(id)}, "id", null, null, null);
while (cursor.moveToNext()) {
int ID = cursor.getInt(0);
Log.d(TAG,"ID == "+ID);
String name = cursor.getString(1);
Log.d(TAG,"name == "+name);
int age = cursor.getInt(2);
Log.d(TAG,"age == "+age);
}
cursor.close();
db.close();
}
4.3 事務使用
使用db.beginTransaction(); db.setTransactionSuccessful(); db.endTransaction();三個方法來進行事務的處理,
簡單的一個測驗類
// 測驗一個資料庫事物
@Test
public void testTransaction() {
DatabaseHelper dbh = new DatabaseHelper(appContext);
SQLiteDatabase db = dbh.getWritableDatabase();
db.beginTransaction();
Log.d(TAG,"事物開啟!");
try {
db.execSQL("update " + Constants.TABLE_NAME +" set age = 114 where id = 1");
int i = 10 / 0;
db.execSQL("update " + Constants.TABLE_NAME +" set age = 114 where id = 2");
db.setTransactionSuccessful();
Log.d(TAG,"事物成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
db.endTransaction();
db.close();
Log.d(TAG,"事物關閉!");
}
dao.showByAPI(1);
dao.showByAPI(2);
}
看看logcat,首先是進入了 事物開啟,然后程式進入了try中,因為除以了一個0所以報錯,捕獲例外了之后就是進入到finally中關閉了事務,可以發現我們sql中的資訊都回滾了,沒有改變,

我們把int i = 10 / 0;刪了試一試,可以看到成功執行事物,

值得注意的是,事物開啟之后,僅有當前的db物件可以執行sql陳述句,使用Dao類中的方法是無法進行增刪改查的,因為對這些得到的db物件上了鎖!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/413504.html
標籤:其他
