女人自慰AV免费观看内涵网,日韩国产剧情在线观看网址,神马电影网特片网,最新一级电影欧美,在线观看亚洲欧美日韩,黄色视频在线播放免费观看,ABO涨奶期羡澄,第一导航fulione,美女主播操b

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

基于鴻蒙分布式數(shù)據(jù)服務(wù)開發(fā)的聊天室應(yīng)用

OpenHarmony技術(shù)社區(qū) ? 來源:鴻蒙技術(shù)社區(qū) ? 作者:梁青松 ? 2021-11-15 09:32 ? 次閱讀

之前給大家介紹過《HarmonyOS 分布式之仿抖音應(yīng)用》,此次給大家介紹一下基于鴻蒙分布式數(shù)據(jù)服務(wù)開發(fā)的聊天室應(yīng)用,模擬現(xiàn)實中的聊天室對話,可以與小伙伴們互動、分享自己的故事給小伙伴。

主要知識點

分布式數(shù)據(jù)服務(wù)

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/database-mdds-guidelines-0000000000030122

官方介紹:分布式數(shù)據(jù)服務(wù)主要實現(xiàn)用戶設(shè)備中應(yīng)用程序的數(shù)據(jù)內(nèi)容的分布式同步。

當設(shè)備 1 上的應(yīng)用 A 在分布式數(shù)據(jù)庫中增、刪、改數(shù)據(jù)后,設(shè)備 2 上的應(yīng)用 A 也可以獲取到該數(shù)據(jù)庫變化,總結(jié)一句話:多個設(shè)備共用一個數(shù)據(jù)庫。

主頁代碼

沒有特別復(fù)雜的邏輯,主要是分布式數(shù)據(jù)服務(wù)的使用,關(guān)鍵地方都有注釋。
importcom.ldd.myapp.bean.ChatDataBean;
importcom.ldd.myapp.provider.ChatProvider;
importcom.ldd.myapp.util.Tools;
importohos.aafwk.ability.AbilitySlice;
importohos.aafwk.content.Intent;
importohos.agp.components.Button;
importohos.agp.components.ListContainer;
importohos.agp.components.TextField;
importohos.app.Context;
importohos.bundle.IBundleManager;
importohos.data.distributed.common.*;
importohos.data.distributed.user.SingleKvStore;
importohos.utils.zson.ZSONArray;
importohos.utils.zson.ZSONObject;

importjava.util.ArrayList;
importjava.util.List;

importstaticohos.security.SystemPermission.DISTRIBUTED_DATASYNC;

/**
*主頁
*/
publicclassMainAbilitySliceextendsAbilitySlice{
privateContextmContext;
//聊天列表
privateListContainerlcList;
//聊天數(shù)據(jù)
privatefinalListlistData=newArrayList<>();
//聊天數(shù)據(jù)適配器
privateChatProviderchatProvider;
//輸入框
privateTextFieldtfContent;
//發(fā)送按鈕
privateButtonbtnSend;

//分布式數(shù)據(jù)庫管理器
privateKvManagerkvManager;
//分布式數(shù)據(jù)庫
privateSingleKvStoresingleKvStore;
//數(shù)據(jù)庫名稱
privatestaticfinalStringSTORE_NAME="ChatStore";
//存入的列表數(shù)據(jù)key
privatestaticfinalStringKEY_DATA="key_data";
//存入的頭像索引
privatestaticfinalStringKEY_PIC_INDEX="key_pic_index";
privateintpicIndex=0;

@Override
publicvoidonStart(Intentintent){
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
mContext=this;
requestPermission();
initComponent();
initDatabase();
}

/**
*請求分布式權(quán)限
*/
privatevoidrequestPermission(){
if(verifySelfPermission(DISTRIBUTED_DATASYNC)!=IBundleManager.PERMISSION_GRANTED){
if(canRequestPermission(DISTRIBUTED_DATASYNC)){
requestPermissionsFromUser(newString[]{DISTRIBUTED_DATASYNC},0);
}
}
}

/**
*初始化組件
*/
privatevoidinitComponent(){
lcList=(ListContainer)findComponentById(ResourceTable.Id_lc_list);
tfContent=(TextField)findComponentById(ResourceTable.Id_tf_content);
tfContent.setAdjustInputPanel(true);
btnSend=(Button)findComponentById(ResourceTable.Id_btn_send);
btnSend.setEnabled(false);

//初始化適配器
chatProvider=newChatProvider(mContext,listData);
lcList.setItemProvider(chatProvider);

//輸入框內(nèi)容變化監(jiān)聽
tfContent.addTextObserver((text,start,before,count)->{
btnSend.setEnabled(text.length()!=0);
});
//點擊發(fā)送按鈕
btnSend.setClickedListener(component->{
Stringcontent=tfContent.getText().trim();
listData.add(newChatDataBean(Tools.getDeviceId(mContext),picIndex,content));
//存入數(shù)據(jù)庫中
singleKvStore.putString(KEY_DATA,ZSONObject.toZSONString(listData));

//清空輸入框
tfContent.setText("");
});
}

/**
*初始化分布式數(shù)據(jù)庫
*/
privatevoidinitDatabase(){
//創(chuàng)建分布式數(shù)據(jù)庫管理器
kvManager=KvManagerFactory.getInstance().createKvManager(newKvManagerConfig(this));

//數(shù)據(jù)庫配置
Optionsoptions=newOptions();
options.setCreateIfMissing(true)//設(shè)置數(shù)據(jù)庫不存在時是否創(chuàng)建
.setEncrypt(false)//設(shè)置數(shù)據(jù)庫是否加密
.setKvStoreType(KvStoreType.SINGLE_VERSION);//數(shù)據(jù)庫類型
//創(chuàng)建分布式數(shù)據(jù)庫
singleKvStore=kvManager.getKvStore(options,STORE_NAME);
//監(jiān)聽數(shù)據(jù)庫數(shù)據(jù)改變
singleKvStore.subscribe(SubscribeType.SUBSCRIBE_TYPE_ALL,newKvStoreObserver(){
@Override
publicvoidonChange(ChangeNotificationchangeNotification){
ListinsertEntries=changeNotification.getInsertEntries();
ListupdateEntries=changeNotification.getUpdateEntries();

//第一次存入數(shù)據(jù),獲取insertEntries
if(insertEntries.size()>0){
for(Entryentry:insertEntries){
if(KEY_DATA.equals(entry.getKey())){
//回調(diào)為非UI線程,需要在UI線程更新UI
getUITaskDispatcher().syncDispatch(()->{
listData.clear();
listData.addAll(ZSONArray.stringToClassList(entry.getValue().getString(),ChatDataBean.class));
chatProvider.notifyDataChanged();
lcList.scrollTo(listData.size()-1);
});
}
}
}elseif(updateEntries.size()>0){
for(Entryentry:updateEntries){
if(KEY_DATA.equals(entry.getKey())){
//回調(diào)為非UI線程,需要在UI線程更新UI
getUITaskDispatcher().syncDispatch(()->{
listData.clear();
listData.addAll(ZSONArray.stringToClassList(entry.getValue().getString(),ChatDataBean.class));
chatProvider.notifyDataChanged();
lcList.scrollTo(listData.size()-1);
});
}
}
}
}
});

try{
picIndex=singleKvStore.getInt(KEY_PIC_INDEX);
singleKvStore.putInt(KEY_PIC_INDEX,picIndex+1);
}catch(KvStoreExceptione){
e.printStackTrace();
//沒有找到,首次進入
if(e.getKvStoreErrorCode()==KvStoreErrorCode.KEY_NOT_FOUND){
picIndex=0;
singleKvStore.putInt(KEY_PIC_INDEX,picIndex+1);
}
}
}

@Override
protectedvoidonStop(){
super.onStop();
kvManager.closeKvStore(singleKvStore);
}
}

簡單案例

config.json 配置:
"reqPermissions":[
{
"reason":"多設(shè)備協(xié)同",
"name":"ohos.permission.DISTRIBUTED_DATASYNC",
"usedScene":{
"ability":[
"MainAbility"
],
"when":"always"
}
},
{
"name":"ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE"
},
{
"name":"ohos.permission.GET_DISTRIBUTED_DEVICE_INFO"
},
{
"name":"ohos.permission.GET_BUNDLE_INFO"
}
]

布局頁面:

<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">

<Text
ohos:id="$+id:text"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="數(shù)據(jù):0"
ohos:text_size="15fp"/>

<Button
ohos:margin="20vp"
ohos:id="$+id:button"
ohos:height="match_content"
ohos:width="match_parent"
ohos:background_element="$graphic:button_bg"
ohos:padding="10vp"
ohos:text="點擊+1"
ohos:text_color="white"
ohos:text_size="15fp"/>

DirectionalLayout>
MainAbilitySlice 代碼:
importohos.aafwk.ability.AbilitySlice;
importohos.aafwk.content.Intent;
importohos.agp.components.Button;
importohos.agp.components.ListContainer;
importohos.agp.components.Text;
importohos.agp.components.TextField;
importohos.bundle.IBundleManager;
importohos.data.distributed.common.*;
importohos.data.distributed.user.SingleKvStore;
importohos.utils.zson.ZSONArray;

importjava.util.List;

importstaticohos.security.SystemPermission.DISTRIBUTED_DATASYNC;

publicclassMainAbilitySliceextendsAbilitySlice{
//顯示數(shù)據(jù)
privateTexttext;
//分布式數(shù)據(jù)庫管理器
privateKvManagerkvManager;
//分布式數(shù)據(jù)庫
privateSingleKvStoresingleKvStore;
//數(shù)據(jù)庫名稱
privatestaticfinalStringSTORE_NAME="MyStore";
//存入的數(shù)據(jù)key
privatestaticfinalStringKEY_COUNT="key_count";

@Override
publicvoidonStart(Intentintent){
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
requestPermission();
initDatabase();
initComponent();
}

/**
*請求分布式權(quán)限
*/
privatevoidrequestPermission(){
if(verifySelfPermission(DISTRIBUTED_DATASYNC)!=IBundleManager.PERMISSION_GRANTED){
if(canRequestPermission(DISTRIBUTED_DATASYNC)){
requestPermissionsFromUser(newString[]{DISTRIBUTED_DATASYNC},0);
}
}
}

/**
*初始化分布式數(shù)據(jù)庫
*/
privatevoidinitDatabase(){
//創(chuàng)建分布式數(shù)據(jù)庫管理器
kvManager=KvManagerFactory.getInstance().createKvManager(newKvManagerConfig(this));

//數(shù)據(jù)庫配置
Optionsoptions=newOptions();
options.setCreateIfMissing(true)//設(shè)置數(shù)據(jù)庫不存在時是否創(chuàng)建
.setEncrypt(false)//設(shè)置數(shù)據(jù)庫是否加密
.setKvStoreType(KvStoreType.SINGLE_VERSION);//數(shù)據(jù)庫類型
//創(chuàng)建分布式數(shù)據(jù)庫
singleKvStore=kvManager.getKvStore(options,STORE_NAME);
//監(jiān)聽數(shù)據(jù)庫數(shù)據(jù)改變
singleKvStore.subscribe(SubscribeType.SUBSCRIBE_TYPE_ALL,newKvStoreObserver(){
@Override
publicvoidonChange(ChangeNotificationchangeNotification){
ListinsertEntries=changeNotification.getInsertEntries();
ListupdateEntries=changeNotification.getUpdateEntries();

//第一次存入數(shù)據(jù),獲取insertEntries
if(insertEntries.size()>0){
for(Entryentry:insertEntries){
if(KEY_COUNT.equals(entry.getKey())){
//回調(diào)為非UI線程,需要在UI線程更新UI
getUITaskDispatcher().syncDispatch(()->{
intcount=entry.getValue().getInt();
text.setText("數(shù)據(jù):"+count);
});
}
}
}elseif(updateEntries.size()>0){
for(Entryentry:updateEntries){
if(KEY_COUNT.equals(entry.getKey())){
//回調(diào)為非UI線程,需要在UI線程更新UI
getUITaskDispatcher().syncDispatch(()->{
intcount=entry.getValue().getInt();
text.setText("數(shù)據(jù):"+count);
});
}
}
}
}
});

}

/**
*初始化組件
*/
privatevoidinitComponent(){
text=(Text)findComponentById(ResourceTable.Id_text);
Buttonbutton=(Button)findComponentById(ResourceTable.Id_button);

//點擊事件
button.setClickedListener(component->{
try{
intcount=singleKvStore.getInt(KEY_COUNT);
singleKvStore.putInt(KEY_COUNT,count+1);
}catch(KvStoreExceptione){
e.printStackTrace();
//沒有找到,首次進入
if(e.getKvStoreErrorCode()==KvStoreErrorCode.KEY_NOT_FOUND){
intcount=0;
singleKvStore.putInt(KEY_COUNT,count+1);
}
}
});
}
}
注釋比較詳細,主要注意 2 個點:
  • 獲取數(shù)據(jù)時加入 try catch 塊,處理 key 未找到的情況。

  • 數(shù)據(jù)庫數(shù)據(jù)改變監(jiān)聽回調(diào)是非 UI 線程,如果更新 UI 必須切換到 UI 線程。

以上簡單案例就是讓你快速掌握分布式數(shù)據(jù)服務(wù):多個設(shè)備相同的應(yīng)用之間使用同一個數(shù)據(jù)庫。 項目地址(需要登錄才能看到演示圖):
https://gitee.com/liangdidi/DistributedChatDemo.git

責(zé)任編輯:haq


聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 數(shù)據(jù)
    +關(guān)注

    關(guān)注

    8

    文章

    7237

    瀏覽量

    90914
  • 鴻蒙系統(tǒng)
    +關(guān)注

    關(guān)注

    183

    文章

    2638

    瀏覽量

    67621
  • HarmonyOS
    +關(guān)注

    關(guān)注

    79

    文章

    2010

    瀏覽量

    32030

原文標題:一款鴻蒙分布式聊天室應(yīng)用!

文章出處:【微信號:gh_834c4b3d87fe,微信公眾號:OpenHarmony技術(shù)社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。

收藏 人收藏

    評論

    相關(guān)推薦
    熱點推薦

    分布式存儲數(shù)據(jù)恢復(fù)—虛擬機上hbase和hive數(shù)據(jù)數(shù)據(jù)恢復(fù)案例

    分布式存儲數(shù)據(jù)恢復(fù)環(huán)境: 16臺某品牌R730xd服務(wù)器節(jié)點,每臺服務(wù)器節(jié)點上有數(shù)臺虛擬機。 虛擬機上部署Hbase和Hive數(shù)據(jù)庫。
    的頭像 發(fā)表于 04-17 11:05 ?142次閱讀

    分布式云化數(shù)據(jù)庫有哪些類型

    分布式云化數(shù)據(jù)庫有哪些類型?分布式云化數(shù)據(jù)庫主要類型包括:關(guān)系型分布式數(shù)據(jù)庫、非關(guān)系型分布式數(shù)據(jù)
    的頭像 發(fā)表于 01-15 09:43 ?371次閱讀

    #新年新氣象,大家新年快樂!#AIGC入門及鴻蒙入門

    、配置SDK等。 3. 開發(fā)實踐**: 學(xué)習(xí)鴻蒙系統(tǒng)的架構(gòu)和API,了解其組件化、分布式等特性。 通過官方文檔和社區(qū)資源,學(xué)習(xí)和掌握鴻蒙應(yīng)用的開發(fā)
    發(fā)表于 01-13 10:46

    AIGC入門及鴻蒙入門

    JDK、配置SDK等。 3. 開發(fā)實踐: 學(xué)習(xí)鴻蒙系統(tǒng)的架構(gòu)和API,了解其組件化、分布式等特性。 通過官方文檔和社區(qū)資源,學(xué)習(xí)和掌握鴻蒙應(yīng)用的開發(fā)
    發(fā)表于 01-13 10:32

    基于ptp的分布式系統(tǒng)設(shè)計

    在現(xiàn)代分布式系統(tǒng)中,精確的時間同步對于確保數(shù)據(jù)一致性、系統(tǒng)穩(wěn)定性和性能至關(guān)重要。PTP(Precision Time Protocol)是一種網(wǎng)絡(luò)協(xié)議,用于在分布式系統(tǒng)中實現(xiàn)高精度的時間同步
    的頭像 發(fā)表于 12-29 10:09 ?426次閱讀

    HarmonyOS Next 應(yīng)用元服務(wù)開發(fā)-分布式數(shù)據(jù)對象遷移數(shù)據(jù)文件資產(chǎn)遷移

    設(shè)備文件訪問實現(xiàn)文件的遷移,難以獲取文件同步完成的時間。為了保證更高的成功率,文件的遷移不建議繼續(xù)通過該方式實現(xiàn),推薦使用分布式數(shù)據(jù)對象攜帶資產(chǎn)的方式。開發(fā)者此前通過跨設(shè)備文件訪問實現(xiàn)的文件遷移依然生效
    發(fā)表于 12-24 10:11

    HarmonyOS Next 應(yīng)用元服務(wù)開發(fā)-分布式數(shù)據(jù)對象遷移數(shù)據(jù)權(quán)限與基礎(chǔ)數(shù)據(jù)

    設(shè)備文件訪問實現(xiàn)文件的遷移,難以獲取文件同步完成的時間。為了保證更高的成功率,文件的遷移不建議繼續(xù)通過該方式實現(xiàn),推薦使用分布式數(shù)據(jù)對象攜帶資產(chǎn)的方式。開發(fā)者此前通過跨設(shè)備文件訪問實現(xiàn)的文件遷移依然生效
    發(fā)表于 12-24 09:40

    Testin云測中標銀聯(lián)數(shù)據(jù)服務(wù)有限公司一站測試服務(wù)采購項目

    近日,Testin云測成功中標銀聯(lián)數(shù)據(jù)服務(wù)公司一站測試服務(wù)采購項目!
    的頭像 發(fā)表于 12-16 14:51 ?483次閱讀

    京準電鐘:NTP網(wǎng)絡(luò)授時服務(wù)器在分布式網(wǎng)絡(luò)內(nèi)的應(yīng)用

    京準電鐘:NTP網(wǎng)絡(luò)授時服務(wù)器在分布式網(wǎng)絡(luò)內(nèi)的應(yīng)用
    的頭像 發(fā)表于 11-27 16:09 ?425次閱讀
    京準電鐘:NTP網(wǎng)絡(luò)授時<b class='flag-5'>服務(wù)</b>器在<b class='flag-5'>分布式</b>網(wǎng)絡(luò)內(nèi)的應(yīng)用

    分布式光纖測溫是什么?應(yīng)用領(lǐng)域是?

    時,該處的散射光特性會受到影響。通過高速信號采集與數(shù)據(jù)處理技術(shù),可以準確地定位發(fā)生溫度變化的位置,并給出實時的溫度信息。簡而言之,分布式光纖測溫技術(shù)將整條傳輸光纖作為傳感器,光纖上的每一點都兼具“傳”和“感”
    的頭像 發(fā)表于 10-24 15:30 ?1028次閱讀
    <b class='flag-5'>分布式</b>光纖測溫是什么?應(yīng)用領(lǐng)域是?

    分布式存儲費用高嗎?大概需要多少錢

    分布式存儲的費用是否高,取決于多個因素,包括存儲容量、性能要求、服務(wù)提供商、計費模式等。因此,無法簡單地給出一個“高”或“不高”的答案。通常分布式存儲費用通常包含存儲費用、網(wǎng)絡(luò)費用、增值服務(wù)
    的頭像 發(fā)表于 09-24 10:41 ?501次閱讀

    鴻蒙開發(fā)管理:ohos.account.distributedAccount 分布式帳號管理

    獲取分布式帳號單實例對象。
    的頭像 發(fā)表于 07-08 10:03 ?415次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>開發(fā)</b>管理:ohos.account.distributedAccount <b class='flag-5'>分布式</b>帳號管理

    鴻蒙ArkTS聲明開發(fā):跨平臺支持列表【分布式遷移標識】 通用屬性

    組件的分布式遷移標識,指明了該組件在分布式遷移場景下可以將特定狀態(tài)恢復(fù)到對端設(shè)備。
    的頭像 發(fā)表于 06-07 21:15 ?562次閱讀

    鴻蒙開發(fā)接口數(shù)據(jù)管理:【@ohos.data.distributedDataObject (分布式數(shù)據(jù)對象)】

    本模塊提供管理基本數(shù)據(jù)對象的相關(guān)能力,包括創(chuàng)建、查詢、刪除、修改、訂閱等;同時支持相同應(yīng)用多設(shè)備間的分布式數(shù)據(jù)對象協(xié)同能力。
    的頭像 發(fā)表于 06-07 17:51 ?1706次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>開發(fā)</b>接口<b class='flag-5'>數(shù)據(jù)</b>管理:【@ohos.data.distributedDataObject (<b class='flag-5'>分布式</b><b class='flag-5'>數(shù)據(jù)</b>對象)】

    鴻蒙開發(fā)接口數(shù)據(jù)管理:【@ohos.data.distributedData (分布式數(shù)據(jù)管理)】

    分布式數(shù)據(jù)管理為應(yīng)用程序提供不同設(shè)備間數(shù)據(jù)庫的分布式協(xié)同能力。通過調(diào)用分布式數(shù)據(jù)各個接口,應(yīng)用程
    的頭像 發(fā)表于 06-07 09:30 ?1396次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>開發(fā)</b>接口<b class='flag-5'>數(shù)據(jù)</b>管理:【@ohos.data.distributedData (<b class='flag-5'>分布式</b><b class='flag-5'>數(shù)據(jù)</b>管理)】