002 使用鸿蒙WebView创建简单浏览器 step 2 原创

发布于 2021-4-19 21:43
浏览
4收藏

通过DataAbility给简易浏览器增加历史记录存储

学习鸿蒙的点滴记录在Gitee上面:https://gitee.com/javaaier/HarmonyOSNotes

  • 注:本次笔记及代码中使用了发发老师(钟洪发)视频的配套代码.侵删.
  1. 添加数据库类

    @Database(entities = {WebViewHistory.class, WebViewFavorite.class}, version = 1)
    public abstract class WebViewStore extends OrmDatabase {
    }
    
    • 1.
    • 2.
    • 3.
  2. 添加历史记录类

    @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方法
    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
  3. 添加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;
    
        }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
    • 20.
    • 21.
    • 22.
    • 23.
    • 24.
    • 25.
    • 26.
    • 27.
    • 28.
    • 29.
    • 30.
    • 31.
    • 32.
    • 33.
    • 34.
    • 35.
    • 36.
    • 37.
    • 38.
    • 39.
    • 40.
    • 41.
    • 42.
    • 43.
    • 44.
    • 45.
    • 46.
    • 47.
    • 48.
    • 49.
  4. 在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"
          }
        ]
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
    • 20.
    • 21.
    • 22.
    • 23.
  5. 修改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();
                        }
                    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
    • 20.
    • 21.
    • 22.
    • 23.
    • 24.
    • 25.
    • 26.
    • 27.
    • 28.
    • 29.
    • 30.
    • 31.
    • 32.
    • 33.
    • 34.
    • 35.
    • 36.
    • 37.
    • 38.
    • 39.
  6. 运行及验证是否正确

    最终记录和读取的历史记录如下图所示:
    002 使用鸿蒙WebView创建简单浏览器 step 2-开源基础软件社区

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
6
收藏 4
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐