日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OS訪問Data

開發(fā)者可以通過 DataAbilityHelper 類來訪問當前應(yīng)用或其他應(yīng)用提供的共享數(shù)據(jù)。 DataAbilityHelper 作為客戶端,與提供方的 Data 進行通信。Data 接收到請求后,執(zhí)行相應(yīng)的處理,并返回結(jié)果。DataAbilityHelper 提供了一系列與 Data Ability 對應(yīng)的方法。

創(chuàng)新互聯(lián)建站專注于彭州企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,成都商城網(wǎng)站開發(fā)。彭州網(wǎng)站建設(shè)公司,為彭州等地區(qū)提供建站服務(wù)。全流程按需定制,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)

下面介紹 DataAbilityHelper 具體的使用步驟。

聲明使用權(quán)限

如果待訪問的 Data 聲明了訪問需要權(quán)限,則訪問此 Data 需要在配置文件中聲明需要此權(quán)限。聲明請參考權(quán)限申請字段說明。

"reqPermissions": [
    {
        "name": "com.example.myapplication5.DataAbility.DATA"
    }
]

創(chuàng)建 DataAbilityHelper

DataAbilityHelper為開發(fā)者提供了creator()方法來創(chuàng)建DataAbilityHelper實例。該方法為靜態(tài)方法,有多個重載。最常見的方法是通過傳入一個context對象來創(chuàng)建DataAbilityHelper對象。

獲取helper對象示例:

DataAbilityHelper helper = DataAbilityHelper.creator(this);

訪問Data Ability

DataAbilityHelper 為開發(fā)者提供了一系列的接口來訪問不同類型的數(shù)據(jù)(文件、數(shù)據(jù)庫等)。

  • 訪問文件

DataAbilityHelper 為開發(fā)者提供了 FileDescriptor openFile (Uri uri, String mode)方法來操作文件。此方法需要傳入兩個參數(shù),其中 uri 用來確定目標資源路徑,mode 用來指定打開文件的方式,可選方式包含“r”(讀), “w”(寫), “rw”(讀寫),“wt”(覆蓋寫),“wa”(追加寫),“rwt”(覆蓋寫且可讀)。

該方法返回一個目標文件的 FD(文件描述符),把文件描述符封裝成流,開發(fā)者就可以對文件流進行自定義處理。

訪問文件示例:

  // 讀取文件描述符
  FileDescriptor fd = helper.openFile(uri, "r");
  FileInputStream fis = new FileInputStream(fd);

  • 訪問數(shù)據(jù)庫

DataAbilityHelper 為開發(fā)者提供了增、刪、改、查以及批量處理等方法來操作數(shù)據(jù)庫。

方法 描述
ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) 查詢數(shù)據(jù)庫
int insert(Uri uri, ValuesBucket value) 向數(shù)據(jù)庫中插入單條數(shù)據(jù)
int batchInsert(Uri uri, ValuesBucket[] values) 向數(shù)據(jù)庫中插入多條數(shù)據(jù)
int delete(Uri uri, DataAbilityPredicates predicates) 刪除一條或多條數(shù)據(jù)
int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) 更新數(shù)據(jù)庫
DataAbilityResult[] executeBatch(ArrayList operations) 批量操作數(shù)據(jù)庫

這些方法的使用說明如下:

  • query()

查詢方法,其中 uri 為目標資源路徑,columns 為想要查詢的字段。開發(fā)者的查詢條件可以通過 DataAbilityPredicates 來構(gòu)建。查詢用戶表中 id 在 101-103 之間的用戶,并把結(jié)果打印出來,代碼示例如下:

    DataAbilityHelper helper = DataAbilityHelper.creator(this);

     
    // 構(gòu)造查詢條件
    DataAbilityPredicates predicates = new DataAbilityPredicates();
    predicates.between("userId", 101, 103);

     
    // 進行查詢
    ResultSet resultSet = helper.query(uri,columns,predicates);

     
    // 處理結(jié)果
    resultSet.goToFirstRow();
    do{
        // 在此處理ResultSet中的記錄;
    }while(resultSet.goToNextRow());

  • insert()

新增方法,其中 uri 為目標資源路徑,ValuesBucket 為要新增的對象。插入一條用戶信息的代碼示例如下:

    DataAbilityHelper helper = DataAbilityHelper.creator(this);

     
    // 構(gòu)造插入數(shù)據(jù)
    ValuesBucket valuesBucket = new ValuesBucket();
    valuesBucket.putString("name", "Tom");
    valuesBucket.putInteger("age", 12);
    helper.insert(uri, valuesBucket);

  • batchInsert()

批量插入方法,和 insert() 類似。批量插入用戶信息的代碼示例如下:

    DataAbilityHelper helper = DataAbilityHelper.creator(this);

     
    // 構(gòu)造插入數(shù)據(jù)
    ValuesBucket[] values = new ValuesBucket[2];
    value[0] = new ValuesBucket();
    value[0].putString("name", "Tom");
    value[0].putInteger("age", 12);
    value[1] = new ValuesBucket();
    value[1].putString("name", "Tom1");
    value[1].putInteger("age", 16);
    helper.batchInsert(uri, values);

  • delete()

刪除方法,其中刪除條件可以通過 DataAbilityPredicates 來構(gòu)建。刪除用戶表中 id 在 101-103 之間的用戶,代碼示例如下:

    DataAbilityHelper helper = DataAbilityHelper.creator(this);

     
    // 構(gòu)造刪除條件
    DataAbilityPredicates predicates = new DataAbilityPredicates();
    predicates.between("userId", 101,103);
    helper.delete(uri,predicates);

  • update()

更新方法,更新數(shù)據(jù)由 ValuesBucket 傳入,更新條件由 DataAbilityPredicates 來構(gòu)建。更新 id 為 102 的用戶,代碼示例如下:

    DataAbilityHelper helper = DataAbilityHelper.creator(this);

     
    // 構(gòu)造更新條件
    DataAbilityPredicates predicates = new DataAbilityPredicates();
    predicates.equalTo("userId",102);

     
    // 構(gòu)造更新數(shù)據(jù)
    ValuesBucket valuesBucket = new ValuesBucket();
    valuesBucket.putString("name", "Tom");
    valuesBucket.putInteger("age", 12);
    helper.update(uri, valuesBucket, predicates);

  • executeBatch()

此方法用來執(zhí)行批量操作。DataAbilityOperation 中提供了設(shè)置操作類型、數(shù)據(jù)和操作條件的方法,開發(fā)者可自行設(shè)置自己要執(zhí)行的數(shù)據(jù)庫操作。插入多條數(shù)據(jù)的代碼示例如下:

    DataAbilityHelper helper = DataAbilityHelper.creator(abilityObj, insertUri);

     
    // 構(gòu)造批量操作
    ValuesBucket value1 = initSingleValue();
    DataAbilityOperation opt1 = DataAbilityOperation.newInsertBuilder(insertUri).withValuesBucket(value1).build();
    ValuesBucket value2 = initSingleValue2();
    DataAbilityOperation opt2 = DataAbilityOperation.newInsertBuilder(insertUri).withValuesBucket(value2).build();
    ArrayList operations = new ArrayList();
    operations.add(opt1);
    operations.add(opt2);
    DataAbilityResult[] result = helper.executeBatch(insertUri, operations);

網(wǎng)站欄目:創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OS訪問Data
本文路徑:http://www.5511xx.com/article/dpphphe.html