看本文章之前需要先熟悉一下原子化服務特征和流程 HarmonyOS-5分鐘教會你原子化服務。
服務卡片(以下簡稱“卡片”)是 FA 的一種界面展示形式,將 FA 的重要信息或操作前置到卡片,以達到服務直達,減少體驗層級的目的。
卡片常用于嵌入到其他應用(當前只支持系統應用)中作為其界面的一部分顯示,并支持拉起頁面,發送消息等基礎的交互功能。卡片使用方負責顯示卡片。
卡片提供方:開發者僅需作為卡片提供方進行服務卡片內容的開發,控制卡片實際顯示的內容、控件布局以及控件點擊事件。
卡片使用方:顯示卡片內容的宿主應用,控制卡片在宿主中展示的位置。
卡片管理服務:用于管理系統中所添加卡片的常駐代理服務,包括卡片對象的管理與使用,以及卡片周期性刷新等。
說明:卡片使用方和提供方不要求常駐運行,在需要添加/刪除/請求更新卡片時,卡片管理服務會拉起卡片提供方獲取卡片信息。
服務卡片的運作機制
先上圖:
-
周期性刷新:在卡片添加后,根據卡片的刷新策略啟動定時任務周期性觸發卡片的刷新。
-
卡片緩存管理:在卡片添加到卡片管理服務后,對卡片的視圖信息進行緩存,以便下次獲取卡片時可以直接返回緩存數據,降低時延。
-
卡片生命周期管理:對于卡片切換到后臺或者被遮擋時,暫停卡片的刷新;以及卡片的升級/卸載場景下對卡片數據的更新和清理。
-
卡片使用方對象管理:對卡片使用方的 RPC 對象進行管理,用于使用方請求進行校驗以及對卡片更新后的回調處理。
-
通信適配層:負責與卡片使用方和提供方進行 RPC 通信。
卡片提供方包含以下模塊:
-
卡片服務:由卡片提供方開發者實現,開發者實現 onCreateForm、onUpdateForm 和 onDeleteForm 處理創建卡片、更新卡片以及刪除卡片等請求,提供相應的卡片服務。
-
卡片提供方實例管理模塊:由卡片提供方開發者實現,負責對卡片管理服務分配的卡片實例進行持久化管理。
服務卡片開發簡介
關于服務卡片的接口說明,Java 卡片與 JS 卡片選型,約束與限制可以去官網上查看:
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ability-service-widget-provider-intro-0000001150722105
Java 卡片開發
這次先來講解 Java 卡片開發,后期會專門用一篇來講解 JS 卡片開發。
使用 DevEco Studio 創建卡片工程(前面文章已經說明,這里不再累述)。
我們先看看配置文件 config.json:
"forms":[
{
"landscapeLayouts":[
"$layout:form_weather_widget_2_2"
],
"isDefault":true,
"scheduledUpdateTime":"10:30",
"defaultDimension":"2*2",
"name":"widget",
"description":"Thisisaservicewidget",
"colorMode":"auto",
"type":"Java",
"supportDimensions":[
"2*2"
],
"portraitLayouts":[
"$layout:form_weather_widget_2_2"
],
"updateEnabled":true,
"updateDuration":1
}
],
①“type”:默認值是 JS,我們需要更改為“Java”代表是一個 Java 卡片。
②“scheduledUpdateTime”:表示卡片的定點刷新的時刻,采用 24 小時制,精確到分鐘。但是我在設置時間點的時候并沒有更新,具體原因待考察。
"scheduledUpdateTime":"10:30",
③“updateEnabled”:表示卡片是否支持周期性刷新,取值范圍:
-
true:表示支持周期性刷。
-
false:表示不支持周期性刷新。
④“updateDuration”:表示卡片定時刷新的更新周期,單位為 30 分鐘,取值為自然數。
-
當取值為 0 時,表示該參數不生效。
-
當取值為正整數 N 時,表示刷新周期為 30*N 分鐘。
"updateEnabled":true,
"updateDuration":1
設置卡片定時刷新,每 30 分鐘更新一次。
⑤“supportDimensions”:表示卡片支持的外觀規格,取值范圍:
-
1*2:表示 1 行 2 列的二宮格。
-
2*2:表示 2 行 2 列的四宮格。
-
2*4:表示 2 行 4 列的八宮格。
-
4*4:表示 4 行 4 列的十六宮格。
⑥“portraitLayouts”:表示卡片外觀規格對應的豎向布局文件,與 supportDimensions 中的規格一一對應。僅當卡片類型為 Java 卡片時,需要配置該標簽。
⑦“landscapeLayouts”:表示卡片外觀規格對應的橫向布局文件,與 supportDimensions 中的規格一一對應。僅當卡片類型為 Java 卡片時,需要配置該標簽。
MainAbility 中覆寫卡片相關回調函數:
-
onCreateForm(Intent intent)
-
onUpdateForm(long formId)
-
onDeleteForm(long formId)
-
onCastTempForm(long formId)
-
onEventNotify(Map
formEvents) -
onAcquireFormState(Intent intent)
當卡片使用方請求獲取卡片時,卡片提供方會被拉起并調用 onCreateForm(Intent intent) 回調,intent 中會帶有卡片 ID,卡片名稱,臨時卡片標記和卡片外觀規格信息。
代碼如下:
protectedProviderFormInfoonCreateForm(Intentintent){
HiLog.info(TAG,"onCreateForm");
//卡片id
longformId=intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY,INVALID_FORM_ID);
//卡片名稱
StringformName=intent.getStringParam(AbilitySlice.PARAM_FORM_NAME_KEY);
//卡片規格
intdimension=intent.getIntParam(AbilitySlice.PARAM_FORM_DIMENSION_KEY,DEFAULT_DIMENSION_2X2);
HiLog.info(TAG,"onCreateForm:formId="+formId+",formName="+formName+",dimension="+dimension);
//將卡片信息存入數據庫
saveFormInfo(formId,formName,dimension);
//開發者需要根據卡片的名稱以及外觀規格獲取對應的xml布局并構造卡片對象,此處ResourceTable.Layout_form_weather_widget_2_2
ProviderFormInfoformInfo=newProviderFormInfo(ResourceTable.Layout_form_weather_widget_2_2,this);
//獲取此 ProviderFormInfo 對象中包含的ComponentProvider數據。
ComponentProvidercomponentProvider=formInfo.getComponentProvider();
//設置組件的文本內容
componentProvider.setText(ResourceTable.Id_weather_text,"天氣:多云");
componentProvider.setText(ResourceTable.Id_weather_temperature,"溫度:29度");
componentProvider.setText(ResourceTable.Id_weather_ph,"PH值:2.9");
//將ComponentProvider中指定的操作合并到此ProviderFormInfo對象中包含的ComponentProvider對象中
formInfo.mergeActions(componentProvider);
returnformInfo;
}
布局:form_weather_widget_2_2.xml
<DependentLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#FFFFFFFF"
ohos:remote="true">
<Image
ohos:height="match_parent"
ohos:width="126vp"
ohos:horizontal_center="true"
ohos:image_src="$media:weather"
ohos:scale_mode="zoom_start"
ohos:top_margin="17vp"/>
<DirectionalLayout
ohos:height="match_content"
ohos:width="match_content"
ohos:align_parent_bottom="true"
ohos:bottom_margin="12vp"
ohos:horizontal_center="true"
ohos:orientation="vertical">
<Text
ohos:id="$+id:weather_text"
ohos:height="match_content"
ohos:width="match_parent"
ohos:text="天氣:晴"
ohos:text_color="#E5000000"
ohos:text_size="12fp"
ohos:text_weight="400"
ohos:top_margin="2vp"
ohos:truncation_mode="ellipsis_at_end"/>
<Text
ohos:id="$+id:weather_temperature"
ohos:height="match_content"
ohos:width="match_parent"
ohos:text="溫度:25度"
ohos:text_color="#99000000"
ohos:text_size="12fp"
ohos:text_weight="400"
ohos:top_margin="2vp"
ohos:truncation_mode="ellipsis_at_end"/>
<Text
ohos:id="$+id:weather_ph"
ohos:height="match_content"
ohos:width="match_parent"
ohos:text="PH值:2.5"
ohos:text_color="#99000000"
ohos:text_size="12fp"
ohos:text_weight="400"
ohos:top_margin="2vp"
ohos:truncation_mode="ellipsis_at_end"/>
DirectionalLayout>
DependentLayout>

示例如下:
protectedvoidonUpdateForm(longformId){
HiLog.info(TAG,"onUpdateForm");
super.onUpdateForm(formId);
refeshData();
}
/**
*updateforms
*/
privatevoidrefeshData(){
//獲取卡片集合
ListformList=DatabaseUtils.queryForms(this,null);
for(FormInfoformInfo:formList){
ProviderFormInforefesh=newProviderFormInfo(ResourceTable.Layout_form_weather_widget_2_2,this);
ComponentProvidercomponentProvider=refesh.getComponentProvider();
//這里更新的值,實際使用中可根據自己項目要求設置,比如:隨機獲取一個值
componentProvider.setText(ResourceTable.Id_weather_text,"天氣:多云轉晴");
componentProvider.setText(ResourceTable.Id_weather_temperature,"溫度:30度");
componentProvider.setText(ResourceTable.Id_weather_ph,"PH值:3.0");
try{
//卡片提供方主動更新卡片
updateForm(formInfo.getFormId(),componentProvider);
}catch(FormExceptione){
HiLog.error(TAG,"FormException");
}
}
}
定時更新效果:
總結
實際項目中需要通過網絡獲取數據,這里只是簡單的模擬數據。只是簡單說明了服務卡片創建的回調方法實現,定點和定時數據更新的時機和回調方法的實現,下期會更新關于服務卡片信息持久化、卡片控制事件。
審核編輯 :李倩
-
JAVA
+關注
關注
20文章
2983瀏覽量
106467 -
HarmonyOS
+關注
關注
79文章
2005瀏覽量
31773
原文標題:用Java開發HarmonyOS服務卡片
文章出處:【微信號:gh_834c4b3d87fe,微信公眾號:OpenHarmony技術社區】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
HarmonyOS NEXT 原生應用/元服務調試概述
HarmonyOS開發指導類文檔更新速遞(下)

評論