通過DataAbility給簡易瀏覽器增加歷史記錄存儲
學習鴻蒙的點滴記錄在Gitee上面:gitee.com/javaaier/Ha…
- 注:本次筆記及代碼中使用了發發老師(鍾洪發)視頻的配套代碼.侵刪.
-
添加數據庫類
@Database(entities = {WebViewHistory.class, WebViewFavorite.class}, version = 1) public abstract class WebViewStore extends OrmDatabase { }
-
添加歷史記錄類
@Entity(tableName = "WebViewHistory", indices = {@Index(value = {"historyId"}, name = "historyId_index", unique = true)}) public class WebViewHistory extends OrmObject{ @PrimaryKey(autoGenerate = true) private Integer historyId; private Long browseTime; private String url; private String title; ... 省略了 getters setters toString方法 }
-
添加Data Ability,並實現增加和查詢的方法
public class WebViewHistoryAbility extends Ability { private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo"); private OrmContext ormContext = null; @Override public void onStart(Intent intent) { super.onStart(intent); HiLog.info(LABEL_LOG, "WebViewHistoryAbility onStart"); DatabaseHelper databaseHelper = new DatabaseHelper(this); ormContext = databaseHelper.getOrmContext("WebViewStore", "WebViewStore.db", WebViewStore.class, null); } @Override public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) { HiLog.info(LABEL_LOG, "WebViewHistoryAbility query"); if (ormContext == null) { return null; } System.out.println("----------------------------開始獲取數據--------------------------"); //真正查詢數據的語句 OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates, WebViewHistory.class); ResultSet rs = ormContext.query(ormPredicates, columns); return rs; } @Override public int insert(Uri uri, ValuesBucket value) { HiLog.info(LABEL_LOG, "WebViewHistoryAbility insert"); if (ormContext == null) { return -1; } System.out.println("----------------------------開始寫入數據--------------------------"); WebViewHistory history = new WebViewHistory(); history.setBrowseTime(value.getLong("browseTime")); history.setTitle(value.getString("title")); history.setUrl(value.getString("url")); boolean insert = ormContext.insert(history); if (!insert) { return -1; } boolean flush = ormContext.flush(); if (!flush) { return -1; } int id = Math.toIntExact(history.getRowId()); System.out.println("----------------------------寫入數據完成--------------------------"); return id; }
-
在config.json文件中添加權限
"reqPermissions": [ { "name": "ohos.permission.INTERNET" }, { "name": "ohos.permission.GET_DISTRIBUTED_DEVICE_INFO" }, { "name": "ohos.permission.DISTRIBUTED_DATASYNC" }, { "name": "ohos.permission.READ_USER_STORAGE" }, { "name": "com.javaaier.family.huawei.DataAbilityShellProvider.PROVIDER" } ], "defPermissions": [ { "name": "com.javaaier.family.huawei.DataAbilityShellProvider.PROVIDER", "grantMode": "system_grant" } ]
-
修改WebView界面,增加查看歷史記錄的按鈕(本次筆記只記錄輸出歷史記錄到控制台)
//定義歷史記錄Data Ability的Uri historyUri = Uri.parse("dataability:///com.javaaier.family.huawei.WebViewHistoryAbility"); //新增歷史記錄 ValuesBucket values = new ValuesBucket(); values.putLong("browseTime", System.currentTimeMillis()); values.putString("url", "url"); values.putString("title", webView.getTitle()); try { int insertRowId = helper.insert(historyUri, values); } catch (DataAbilityRemoteException e) { e.printStackTrace(); } // 查看歷史記錄 case ResourceTable.Id_button_webView_viewHistory: { try { DataAbilityPredicates dap = new DataAbilityPredicates(); dap.orderByDesc("browseTime"); ResultSet rs = helper.query(historyUri, new String[]{"historyId", "browseTime", "url", "title"}, dap); Utils.showTip(SimpleWebViewAbilitySlice.this, "已讀取數據"); if (rs.getRowCount() > 0) { rs.goToFirstRow(); for (int i = 0; i < rs.getRowCount(); i++) { System.out.println("查詢出來的數據:" + rs.getInt(rs.getColumnIndexForName("historyId")) + "," + rs.getLong(rs.getColumnIndexForName("browseTime")) + "," + rs.getString(rs.getColumnIndexForName("url")) + "," + rs.getString(rs.getColumnIndexForName("title"))); rs.goToNextRow(); } } } catch (DataAbilityRemoteException e) { e.printStackTrace(); } }
-
運行及驗證是否正確
最終記錄和讀取的歷史記錄如下圖所示:
作者:人工智能姬
想了解更多內容,請訪問51CTO和華為合作共建的鴻蒙社區:https://harmonyos.51cto.com