浦东做网站,淘宝网站建设方案模板,标准件做啥网站,来宾网站制作公司文章目录 创建新项目启动Serviceets获取service的bundleName DataAbility开发指导开发Data步骤创建Data 创建新项目
ServiceAbility开发指导 在config.json中也有配置出现
启动Service
ets获取service的bundleName 项目的bundleName service的bundleName 这里serviceAbil… 文章目录 创建新项目启动Serviceets获取service的bundleName DataAbility开发指导开发Data步骤创建Data 创建新项目
ServiceAbility开发指导 在config.json中也有配置出现
启动Service
ets获取service的bundleName 项目的bundleName service的bundleName 这里serviceAbility的bundleName是也是项目的bundleName 这里的abilityName是项目的bundleNameserviceAbility的Name组合而成 示例 比如我上图的示例是com.example.myapplication和com.example.myapplication.ServiceAbility1 那可以这么来启动serviceAbility onPageShow(){ //Page中的项目初始化(我们这里初始化就开始调用service)console.info(Index页面显示了)let promise featureAbility.startAbility({want:{bundleName: com.example.myapplication,abilityName: com.example.myapplication.ServiceAbility1,},});
}//serviceAbility会先执行onStart生命周期
//service.ts(我是这样写serviceAbility的)
//export default {
// onStart() {// hilog.info(0x0000, testTag, %{public}s, ServiceAbility onStart);
// console.info(serive1 start)
// },
// ....
//}DataAbility开发指导 接口名描述onInitialized在Ability初始化调用通过此回调方法执行rdb等初始化操作。update更新数据库中的数据。query查询数据库中的数据。delete删除一条或多条数据。normalizeUri对uri进行规范化。一个规范化的uri可以支持跨设备使用、持久化、备份和还原等当上下文改变时仍然可以引用到相同的数据项。batchInsert向数据库中插入多条数据。denormalizeUri将一个由normalizeUri生产的规范化uri转换成非规范化的uri。insert向数据中插入一条数据。openFile打开一个文件。getFileTypes获取文件的MIME类型。getType获取uri指定数据相匹配的MIME类型。executeBatch批量操作数据库中的数据。call自定义方法。
开发Data步骤
创建Data
需要实现Data中Insert、Query、Update、Delete接口的业务内容。保证能够满足数据库存储业务的基本需求。BatchInsert与ExecuteBatch接口已经在系统中实现遍历逻辑依赖Insert、Query、Update、Delete接口逻辑来实现数据的批量处理。
//data.ts(鸿蒙文档中的配置)
import hilog from ohos.hilog;
import featureAbility from ohos.ability.featureAbility;
import dataAbility from ohos.data.dataAbility;
import dataRdb from ohos.data.rdb;const TABLE_NAME book;//表名
const STORE_CONFIG {name:book.db}
const SQL_CREATE_TABLE CREATE TABLE IF NOT EXISTS book(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, introduction TEXT NOT NULL) //创建表
let rdbStore: dataRdb.RdbStore undefined //数据存储仓库export default {onInitialized(abilityInfo) {//DataAbility初始化调用// hilog.info(0x0000, testTag, %{public}s, DataAbility onInitialized);console.info(DataAbility onInitialized, abilityInfo: abilityInfo.bundleName)let context featureAbility.getContext()//获取当前的对象dataRdb.getRdbStore(context, STORE_CONFIG, 1, (err, store) {console.info(DataAbility getRdbStore callback)store.executeSql(SQL_CREATE_TABLE, [])rdbStore store});},insert(uri, valueBucket, callback) {console.info(DataAbility insert start)rdbStore.insert(TABLE_NAME, valueBucket, callback)},batchInsert(uri, valueBuckets, callback) {console.info(DataAbility batch insert start)for (let i 0;i valueBuckets.length; i) {console.info(DataAbility batch insert i i)if (i valueBuckets.length - 1) {rdbStore.insert(TABLE_NAME, valueBuckets[i], (err: any, num: number) {console.info(DataAbility batch insert ret num)})} else {rdbStore.insert(TABLE_NAME, valueBuckets[i], callback)}}},query(uri, columns, predicates, callback) {console.info(DataAbility query start)let rdbPredicates dataAbility.createRdbPredicates(TABLE_NAME, predicates)rdbStore.query(rdbPredicates, columns, callback)},update(uri, valueBucket, predicates, callback) {console.info(DataAbilityupdate start)let rdbPredicates dataAbility.createRdbPredicates(TABLE_NAME, predicates)rdbStore.update(valueBucket, rdbPredicates, callback)},delete(uri, predicates, callback) {console.info(DataAbilitydelete start)let rdbPredicates dataAbility.createRdbPredicates(TABLE_NAME, predicates)rdbStore.delete(rdbPredicates, callback)}
};当你用以下调用
//service.ts(其中一个ability)
import hilog from ohos.hilog;import featureAbility from ohos.ability.featureAbility
import ohos_data_ability from ohos.data.dataAbility
import ohos_data_rdb from ohos.data.rdbvar urivar dataability://com.example.myapplication.DataAbility1
var DAHelper featureAbility.acquireDataAbilityHelper(urivar
);
var valuesBucket {name: chentianyu}
var da new ohos_data_ability.DataAbilityPredicates()
var valArray new Array(value1);
var cars new Array({batchInsert1 : value1,});export default {insertOne(){DAHelper.insert(urivar,valuesBucket,(error, data){console.log(DAHelper insert result: data)})},onStart() {// hilog.info(0x0000, testTag, %{public}s, ServiceAbility onStart);console.info(serive1 start)this.insertOne()},onStop() {hilog.info(0x0000, testTag, %{public}s, ServiceAbility onStop);},onCommand(want, startId) {hilog.info(0x0000, testTag, %{public}s, ServiceAbility onCommand);},};启动程序后出现一下日志(Log) 所以我们要看DataAbility接口开发 其实是dataability:///bundleNameName不是dataability://bundleNameName,这bundleName和Name上面有讲(ets获取service的bundleName) 这样,PageAbility、ServiceAbility、DataAbility就可以联动起来了