HarmonyOS基于Java开发的服务卡片

开发 后端 OpenHarmony
本篇介绍HarmonyOS 基于Java开发的服务卡片,服务卡片是将重要信息或操作前置的一种界面展示,目的是减少体验层级,服务直达,增强用户交互体验。

[[418070]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

服务卡片介绍:

服务卡片是将重要信息或操作前置的一种界面展示,目的是减少体验层级,服务直达,增强用户交互体验。

卡片基本信息说明:

  • 卡片使用方

显示卡片内容的宿主应用,控制卡片在宿主中展示的位置。

  • 卡片管理服务

用于管理系统中所添加卡片的常驻代理服务,包括卡片对象的管理与使用,以及卡片周期性刷新等。

  • 卡片提供方

提供卡片显示内容的HarmonyOS应用或原子化服务,控制卡片的显示内容、控件布局以及控件点击事件。

以下是官方提供的运作机制:

项目介绍:

本篇主要对系统时间日期来展示服务卡片,该Demo是Java为基础,无JS相关代码,以下将对项目详细介绍。

项目目录如下:

【中软国际】HarmonyOS 基于Java开发的服务卡片-鸿蒙HarmonyOS技术社区

database中FormBean是所需的bean文件并设置数据库表名,包含卡片的id、名称以及布局id,FormDatabase是bean绑定Orm数据库类。

Slice中MainAbilitySlice与TimeCardSlice是展示页面,MainAbilitySlice是创建项目系统生成,我设置初始化页面为TimeCardSlice。

Utils中是分别是卡片工具类、数据库工具类、日期工具类。

MainAbility以及MyApplication也是创建项目系统生成的。

注意MainAbility中的setMainRoute可以设置指定初始化页面。

TimerAbility是一个服务类,用来更新卡片时间。

配置文件如下:

  1.   "app": { 
  2.     "bundleName""com.fei.yuan.myapplication"
  3.     "vendor""fei"
  4.     "version": { 
  5.       "code": 1000000, 
  6.       "name""1.0.0" 
  7.     } 
  8.   }, 
  9.   "deviceConfig": { 
  10.     "default": { 
  11.       "keepAlive"true 
  12.     } 
  13.   }, 
  14.   "module": { 
  15.     "package""com.fei.yuan.myapplication"
  16.     "name"".MyApplication"
  17.     "mainAbility""com.fei.yuan.myapplication.MainAbility"
  18.     "deviceType": [ 
  19.       "phone" 
  20.     ], 
  21.     "distro": { 
  22.       "deliveryWithInstall"true
  23.       "moduleName""entry"
  24.       "moduleType""entry"
  25.       "installationFree"false 
  26.     }, 
  27.     "reqPermissions": [ 
  28.       { 
  29.         "name""ohos.permission.KEEP_BACKGROUND_RUNNING"
  30.         "reason""keep service ability backgroud running"
  31.         "usedScene": { 
  32.           "ability": [ 
  33.             ".TimerAbility" 
  34.           ], 
  35.           "when""always" 
  36.         } 
  37.       } 
  38.     ], 
  39.     "abilities": [ 
  40.       { 
  41.         "skills": [ 
  42.           { 
  43.             "entities": [ 
  44.               "entity.system.home" 
  45.             ], 
  46.             "actions": [ 
  47.               "action.system.home" 
  48.             ] 
  49.           } 
  50.         ], 
  51.         "orientation""unspecified"
  52.         "name""com.fei.yuan.myapplication.MainAbility"
  53.         "icon""$media:icon"
  54.         "description""$string:mainability_description"
  55.         "launchType""standard"
  56.         "formsEnabled"true
  57.         "label""$string:entry_MainAbility"
  58.         "type""page"
  59.         "visible"true
  60.         "forms": [ 
  61.           { 
  62.             "landscapeLayouts": [ 
  63.               "$layout:form_image_with_info_time_2_2"
  64.               "$layout:form_image_with_info_time_2_4" 
  65.             ], 
  66.             "isDefault"true
  67.             "scheduledUpdateTime""10:30"
  68.             "defaultDimension""2*2"
  69.             "name""widget"
  70.             "description""This is a service widget"
  71.             "colorMode""auto"
  72.             "type""Java"
  73.             "supportDimensions": [ 
  74.               "1*2"
  75.               "2*2"
  76.               "2*4" 
  77.             ], 
  78.             "portraitLayouts": [ 
  79.               "$layout:form_image_with_info_time_2_2"
  80.               "$layout:form_image_with_info_time_2_4"
  81.               "$layout:form_image_with_info_time_1_2" 
  82.             ], 
  83.             "updateEnabled"true
  84.             "updateDuration": 30, 
  85.             "formVisibleNotify"true
  86.             "metaData": { 
  87.               "customizeData": [ 
  88.                 { 
  89.                   "name""needBlurBackgroundForLauncher"
  90.                   "value""true" 
  91.                 } 
  92.               ] 
  93.             } 
  94.           } 
  95.         ] 
  96.       }, 
  97.       { 
  98.         "icon""$media:icon"
  99.         "name""com.fei.yuan.myapplication.TimerAbility"
  100.         "description""$string:timerability_description"
  101.         "type""service"
  102.         "visible"true
  103.         "backgroundModes": [ 
  104.           "dataTransfer"
  105.           "location" 
  106.         ] 
  107.       } 
  108.     ] 
  109.   } 

forms内属性说明:

先上效果图:

代码详解:

下面开始项目解析:

首先介绍MainAbility主页,主要方法有onStart,onCreateForm,onDeleteForm

  1. //主页初始化 
  2. public void onStart(Intent intent) { 
  3.     HiLog.info(TAG, "onStart"); 
  4.     super.onStart(intent); 
  5.     //添加在数据库中添加FormDatabase表 
  6.     ormContext = databaseHelper.getOrmContext("FormBean""FormDatabase.db", FormDatabase.class); 
  7.     // 这里是开启服务,用来即时更新服务卡片时间(后天有说明) 
  8.     Intent intentService = new Intent(); 
  9.     Operation operation = new Intent.OperationBuilder() 
  10.             .withBundleName(getBundleName()) 
  11.             .withAbilityName(TimerAbility.class.getName()) 
  12.             .build(); 
  13.     intentService.setOperation(operation); 
  14.     startAbility(intentService); 
  15.     //加载初始页 
  16.     super.setMainRoute(TimeCardSlice.class.getName()); 
  17.  
  1. //创建卡片 
  2. protected ProviderFormInfo onCreateForm(Intent intent) { 
  3.     HiLog.info(TAG, "onCreateForm"); 
  4.     if (intent == null) { 
  5.         return new ProviderFormInfo(); 
  6.     } 
  7.     //获取卡片id 
  8.     formId = INVALID_FORM_ID; 
  9.     if (intent.hasParameter(AbilitySlice.PARAM_FORM_IDENTITY_KEY)) { 
  10.         formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID); 
  11.     } else { 
  12.         return new ProviderFormInfo(); 
  13.     } 
  14.  
  15.     //获取卡片名称 
  16.     String formName = ""
  17.     if (intent.hasParameter(AbilitySlice.PARAM_FORM_NAME_KEY)) { 
  18.         formName = intent.getStringParam(AbilitySlice.PARAM_FORM_NAME_KEY); 
  19.     } 
  20.  
  21.     //获取卡片规格 
  22.     int dimension = DEFAULT_DIMENSION_2X2; 
  23.     if (intent.hasParameter(AbilitySlice.PARAM_FORM_DIMENSION_KEY)) { 
  24.         dimension = intent.getIntParam(AbilitySlice.PARAM_FORM_DIMENSION_KEY, DEFAULT_DIMENSION_2X2); 
  25.     } 
  26.  
  27.     int layoutId = ResourceTable.Layout_form_image_with_info_time_2_2; 
  28.     if (dimension == DEFAULT_DIMENSION_2X4) { 
  29.         layoutId = ResourceTable.Layout_form_image_with_info_time_2_4; 
  30.     }else if (dimension == DEFAULT_DIMENSION_1X2){ 
  31.         layoutId = ResourceTable.Layout_form_image_with_info_time_1_2; 
  32.     } 
  33.  
  34.     formInfo = new ProviderFormInfo(layoutId, this); 
  35.  
  36.     //存储卡片信息 
  37.     FormBean formBean = new FormBean(formId, formName, dimension); 
  38.     ComponentProvider componentProvider = ComponentProviderUtils.getComponentProvider(formBean, this); 
  39.     formInfo.mergeActions(componentProvider); 
  40.  
  41.     if (ormContext == null) { 
  42.         ormContext = databaseHelper.getOrmContext("FormBean""FormDatabase.db", FormDatabase.class); 
  43.     } 
  44.     try { 
  45.         //保存数据到数据库 
  46.         DatabaseUtils.insertForm(formBean, ormContext); 
  47.     } catch (Exception e) { 
  48.         //删除数据库中保存的数据 
  49.         DatabaseUtils.deleteFormData(formBean.getFormId(), ormContext); 
  50.     } 
  51.  
  52.     return formInfo; 
  1. //删除卡片 
  2. protected void onDeleteForm(long formId) { 
  3.     HiLog.info(TAG, "onDeleteForm: formId=" + formId); 
  4.     super.onDeleteForm(formId); 
  5.     DatabaseUtils.deleteFormData(formId, ormContext); 

接下来就是TimeCardSlice页,进行时间获取展示,并使用计时器来进行刷新页面。

  1. private void initData() { 
  2.     //获取日期 
  3.     String currentDate = DateUtils.getCurrentDate("yyyy-MM-dd"); 
  4.     String[] split = currentDate.split("-"); 
  5.     monthText.setText(split[1]); 
  6.     dayText.setText(split[2]); 
  7.  
  8.     //获取时间 
  9.     Calendar calendar = Calendar.getInstance(); 
  10.     int hour = calendar.get(Calendar.HOUR_OF_DAY); 
  11.     hourText.setText(intToString(hour)); 
  12.     int min = calendar.get(Calendar.MINUTE); 
  13.     minText.setText(intToString(min)); 
  14.     int second = calendar.get(Calendar.SECOND); 
  15.     secondText.setText(intToString(second)); 
  16.  
  17.     //获取星期 
  18.     int week = calendar.get(Calendar.DAY_OF_WEEK); 
  19.     weekText.setText(getWeek(this,week)); 
  20.      
  1. // 计时器刷新数据 
  2. private void startTimer() { 
  3.     timer = new Timer(); 
  4.     timer.schedule(new TimerTask() { 
  5.         @Override 
  6.         public void run() { 
  7.             runnable.run(); 
  8.             myEventHandler.sendEvent(1); 
  9.         } 
  10.     }, 0, PERIOD); 

最后是TimerAbility页,主要是对服务卡片进行刷新操作:

  1. protected void onStart(Intent intent) { 
  2.     HiLog.info(TAG, "onStart"); 
  3.     ormContext = databaseHelper.getOrmContext("FormBean""FormDatabase.db", FormDatabase.class); 
  4.     startTimer(); 
  5.     super.onStart(intent); 

重点是这个方法,进行更新数据。

  1. private void upDateFormBean() { 
  2.     OrmPredicates ormPredicates = new OrmPredicates(FormBean.class); 
  3.     List<FormBean> beanList = ormContext.query(ormPredicates); 
  4.     if (beanList.size() <= 0) { 
  5.         return
  6.     } 
  7.     for (FormBean formBean : beanList) { 
  8.         //更新卡片信息 
  9.         ComponentProvider componentProvider = ComponentProviderUtils.getComponentProvider(formBean,this); 
  10.         try { 
  11.             updateForm(formBean.getFormId(),componentProvider); 
  12.         } catch (FormException e) { 
  13.             DatabaseUtils.deleteFormData(formBean.getFormId(),ormContext); 
  14.         } 
  15.     } 

注意:Java卡片与JS卡片选型指导,官方提倡使用JS来实现卡片,支持的控件和场景都很丰富

Java/JS卡片场景能力差异如下表所示:

综上所述,JS卡片比Java卡片支持的控件和能力都更丰富:

  • Java卡片:适合作为一个直达入口,没有复杂的页面和事件。
  • JS卡片:适合有复杂界面的卡片。

完整代码如下:

MainAbility.java:

  1. public class MainAbility extends Ability { 
  2.     private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x0, "TAG---"); 
  3.     private static final int INVALID_FORM_ID = -1; 
  4.     private static final int DEFAULT_DIMENSION_1X2 = 1; 
  5.     private static final int DEFAULT_DIMENSION_2X2 = 2; 
  6.     private static final int DEFAULT_DIMENSION_2X4 = 3; 
  7.     private static final int DEFAULT_DIMENSION_4X4 = 4; 
  8.     private long formId; 
  9.     private DatabaseHelper databaseHelper = new DatabaseHelper(this); 
  10.     private OrmContext ormContext; 
  11.     private ProviderFormInfo formInfo; 
  12.  
  13.  
  14.     @Override 
  15.     public void onStart(Intent intent) { 
  16.         HiLog.info(TAG, "onStart"); 
  17.         super.onStart(intent); 
  18.         ormContext = databaseHelper.getOrmContext("FormBean""FormDatabase.db", FormDatabase.class); 
  19.         // 开启服务 
  20.         Intent intentService = new Intent(); 
  21.         Operation operation = new Intent.OperationBuilder() 
  22.                 .withBundleName(getBundleName()) 
  23.                 .withAbilityName(TimerAbility.class.getName()) 
  24.                 .build(); 
  25.         intentService.setOperation(operation); 
  26.         startAbility(intentService); 
  27.         super.setMainRoute(TimeCardSlice.class.getName()); 
  28.          
  29.  
  30.     } 
  31.  
  32.     /** 
  33.      * 创建卡片时回调 
  34.      * 
  35.      * @param intent 
  36.      * @return ProviderFormInfo 
  37.      */ 
  38.     @Override 
  39.     protected ProviderFormInfo onCreateForm(Intent intent) { 
  40.         HiLog.info(TAG, "onCreateForm"); 
  41.         if (intent == null) { 
  42.             return new ProviderFormInfo(); 
  43.         } 
  44.         //获取卡片id 
  45.         formId = INVALID_FORM_ID; 
  46.         if (intent.hasParameter(AbilitySlice.PARAM_FORM_IDENTITY_KEY)) { 
  47.             formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID); 
  48.         } else { 
  49.             return new ProviderFormInfo(); 
  50.         } 
  51.  
  52.         //获取卡片名称 
  53.         String formName = ""
  54.         if (intent.hasParameter(AbilitySlice.PARAM_FORM_NAME_KEY)) { 
  55.             formName = intent.getStringParam(AbilitySlice.PARAM_FORM_NAME_KEY); 
  56.         } 
  57.  
  58.         //获取卡片规格 
  59.         int dimension = DEFAULT_DIMENSION_2X2; 
  60.         if (intent.hasParameter(AbilitySlice.PARAM_FORM_DIMENSION_KEY)) { 
  61.             dimension = intent.getIntParam(AbilitySlice.PARAM_FORM_DIMENSION_KEY, DEFAULT_DIMENSION_2X2); 
  62.         } 
  63.  
  64.         int layoutId = ResourceTable.Layout_form_image_with_info_time_2_2; 
  65.         if (dimension == DEFAULT_DIMENSION_2X4) { 
  66.             layoutId = ResourceTable.Layout_form_image_with_info_time_2_4; 
  67.         }else if (dimension == DEFAULT_DIMENSION_1X2){ 
  68.             layoutId = ResourceTable.Layout_form_image_with_info_time_1_2; 
  69.         } 
  70.  
  71.         formInfo = new ProviderFormInfo(layoutId, this); 
  72.  
  73.         //存储卡片信息 
  74.         FormBean formBean = new FormBean(formId, formName, dimension); 
  75.         ComponentProvider componentProvider = ComponentProviderUtils.getComponentProvider(formBean, this); 
  76.         formInfo.mergeActions(componentProvider); 
  77.  
  78.         if (ormContext == null) { 
  79.             ormContext = databaseHelper.getOrmContext("FormBean""FormDatabase.db", FormDatabase.class); 
  80.         } 
  81.         try { 
  82.             DatabaseUtils.insertForm(formBean, ormContext); 
  83.         } catch (Exception e) { 
  84.             DatabaseUtils.deleteFormData(formBean.getFormId(), ormContext); 
  85.         } 
  86.  
  87.         return formInfo; 
  88.     } 
  89.  
  90.     /** 
  91.      * 更新卡片时回调 
  92.      * 
  93.      * @param formId 
  94.      */ 
  95.     @Override 
  96.     protected void onUpdateForm(long formId) { 
  97.         HiLog.info(TAG, "onUpdateForm"); 
  98.         super.onUpdateForm(formId); 
  99.     } 
  100.  
  101.     /** 
  102.      * 请求删除卡片时回调 
  103.      * 
  104.      * @param formId 
  105.      */ 
  106.     @Override 
  107.     protected void onDeleteForm(long formId) { 
  108.         HiLog.info(TAG, "onDeleteForm: formId=" + formId); 
  109.         super.onDeleteForm(formId); 
  110.         DatabaseUtils.deleteFormData(formId, ormContext); 
  111.     } 
  112.  
  113.     @Override 
  114.     protected void onActive() { 
  115.         HiLog.info(TAG, "onActive"); 
  116.         super.onActive(); 
  117.     } 
  118.  
  119.     @Override 
  120.     protected void onInactive() { 
  121.         HiLog.info(TAG, "onInactive"); 
  122.         super.onInactive(); 
  123.     } 
  124.  
  125.     @Override 
  126.     protected void onBackground() { 
  127.         HiLog.info(TAG, "onBackground"); 
  128.         super.onBackground(); 
  129.     } 
  130.  
  131.     @Override 
  132.     protected void onStop() { 
  133.         HiLog.info(TAG, "onStop"); 
  134.         super.onStop(); 
  135.     } 
  136.  
  137.     @Override 
  138.     protected void onForeground(Intent intent) { 
  139.         HiLog.info(TAG, "onForeground"); 
  140.         super.onForeground(intent); 
  141.     } 
  142.  
  143.     @Override 
  144.     protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) { 
  145.         super.onOrientationChanged(displayOrientation); 
  146.     } 

TimeCardSlice.java:

  1. public class TimeCardSlice extends AbilitySlice { 
  2.  
  3.     private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0, TimeCardSlice.class.getName()); 
  4.     private static final Long PERIOD = 1000L; 
  5.     private Text monthText; 
  6.     private Text dayText; 
  7.     private Text hourText; 
  8.     private Text minText; 
  9.     private Text secondText; 
  10.     private Text weekText; 
  11.     private EventRunner eventRunner; 
  12.     private MyEventHandler myEventHandler; 
  13.     private Timer timer; 
  14.     private Runnable runnable = new Runnable() { 
  15.         private void initHandler() { 
  16.             eventRunner = EventRunner.getMainEventRunner(); 
  17.             if (eventRunner == null) { 
  18.                 return
  19.             } 
  20.             myEventHandler = new MyEventHandler(eventRunner); 
  21.         } 
  22.  
  23.         @Override 
  24.         public void run() { 
  25.             initHandler(); 
  26.         } 
  27.     }; 
  28.  
  29.     @Override 
  30.     protected void onStart(Intent intent) { 
  31.         super.onStart(intent); 
  32.         super.setUIContent(ResourceTable.Layout_form_image_with_info_time_2_2); 
  33.  
  34.         initComponent(); 
  35.         initData(); 
  36.         startTimer(); 
  37.     } 
  38.  
  39.     // 计时器刷新数据 
  40.     private void startTimer() { 
  41.         timer = new Timer(); 
  42.         timer.schedule(new TimerTask() { 
  43.             @Override 
  44.             public void run() { 
  45.                 runnable.run(); 
  46.                 myEventHandler.sendEvent(1); 
  47.             } 
  48.         }, 0, PERIOD); 
  49.     } 
  50.  
  51.     private void initComponent() { 
  52.         monthText = (Text) findComponentById(ResourceTable.Id_month); 
  53.         dayText = (Text) findComponentById(ResourceTable.Id_day); 
  54.         weekText = (Text) findComponentById(ResourceTable.Id_week); 
  55.         hourText = (Text) findComponentById(ResourceTable.Id_hour); 
  56.         minText = (Text) findComponentById(ResourceTable.Id_min); 
  57.         secondText = (Text) findComponentById(ResourceTable.Id_second); 
  58.  
  59.     } 
  60.  
  61.     private void initData() { 
  62.         //获取日期 
  63.         String currentDate = DateUtils.getCurrentDate("yyyy-MM-dd"); 
  64.         String[] split = currentDate.split("-"); 
  65.         monthText.setText(split[1]); 
  66.         dayText.setText(split[2]); 
  67.  
  68.         //获取时间 
  69.         Calendar calendar = Calendar.getInstance(); 
  70.         int hour = calendar.get(Calendar.HOUR_OF_DAY); 
  71.         hourText.setText(intToString(hour)); 
  72.         int min = calendar.get(Calendar.MINUTE); 
  73.         minText.setText(intToString(min)); 
  74.         int second = calendar.get(Calendar.SECOND); 
  75.         secondText.setText(intToString(second)); 
  76.  
  77.         //获取星期 
  78.         int week = calendar.get(Calendar.DAY_OF_WEEK); 
  79.         weekText.setText(getWeek(this,week)); 
  80.  
  81.     } 
  82.  
  83.     private class MyEventHandler extends EventHandler { 
  84.  
  85.         public MyEventHandler(EventRunner runner) throws IllegalArgumentException { 
  86.             super(runner); 
  87.         } 
  88.  
  89.         @Override 
  90.         protected void processEvent(InnerEvent event) { 
  91.             super.processEvent(event); 
  92.             int eventId = event.eventId; 
  93.             if (eventId == 1) { 
  94.                 initData(); 
  95.             } 
  96.         } 
  97.     } 
  98.  
  99.     @Override 
  100.     protected void onActive() { 
  101.         super.onActive(); 
  102.     } 
  103.  
  104.     @Override 
  105.     protected void onForeground(Intent intent) { 
  106.         super.onForeground(intent); 
  107.     } 
  108.  
  109.     @Override 
  110.     protected void onStop() { 
  111.         super.onStop(); 
  112.         timer.cancel(); 
  113.     } 

FormBean.java:

  1. @Entity(tableName = "formBean"
  2. public class FormBean extends OrmObject { 
  3.     @PrimaryKey() 
  4.     private Long formId; 
  5.     private String formName; 
  6.     private Integer dimension; 
  7.  
  8.     public FormBean(Long formId, String formName, Integer dimension) { 
  9.         this.formId = formId; 
  10.         this.formName = formName; 
  11.         this.dimension = dimension; 
  12.     } 
  13.  
  14.     public FormBean() { 
  15.     } 
  16.  
  17.     public Long getFormId() { 
  18.         return formId; 
  19.     } 
  20.  
  21.     public void setFormId(Long formId) { 
  22.         this.formId = formId; 
  23.     } 
  24.  
  25.     public String getFormName() { 
  26.         return formName; 
  27.     } 
  28.  
  29.     public void setFormName(String formName) { 
  30.         this.formName = formName; 
  31.     } 
  32.  
  33.     public Integer getDimension() { 
  34.         return dimension; 
  35.     } 
  36.  
  37.     public void setDimension(Integer dimension) { 
  38.         this.dimension = dimension; 
  39.     } 

TimerAbility.java:

  1. public class TimerAbility extends Ability { 
  2.  
  3.     private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x0, "TAG---Service"); 
  4.  
  5.     private static final long PERIOD = 1000L; 
  6.     private static final int NOTIFICATION_ID = 1001; 
  7.     private DatabaseHelper databaseHelper = new DatabaseHelper(this); 
  8.     private OrmContext ormContext; 
  9.  
  10.     @Override 
  11.     protected void onStart(Intent intent) { 
  12.         HiLog.info(TAG, "onStart"); 
  13.  
  14.         ormContext = databaseHelper.getOrmContext("FormBean""FormDatabase.db", FormDatabase.class); 
  15.         startTimer(); 
  16.         super.onStart(intent); 
  17.     } 
  18.  
  19.     private void startTimer(){ 
  20.         Timer timer = new Timer(); 
  21.         timer.schedule(new TimerTask() { 
  22.             @Override 
  23.             public void run() { 
  24.                 upDateFormBean(); 
  25.             } 
  26.         },0,PERIOD); 
  27.     } 
  28.  
  29.     private void upDateFormBean() { 
  30.         OrmPredicates ormPredicates = new OrmPredicates(FormBean.class); 
  31.         List<FormBean> beanList = ormContext.query(ormPredicates); 
  32.         if (beanList.size() <= 0) { 
  33.             return
  34.         } 
  35.         for (FormBean formBean : beanList) { 
  36.             //更新卡片信息 
  37.             ComponentProvider componentProvider = ComponentProviderUtils.getComponentProvider(formBean,this); 
  38.             try { 
  39.                 updateForm(formBean.getFormId(),componentProvider); 
  40.             } catch (FormException e) { 
  41.                 DatabaseUtils.deleteFormData(formBean.getFormId(),ormContext); 
  42.             } 
  43.         } 
  44.     } 
  45.  
  46.  
  47.     private void notice(){ 
  48.         NotificationRequest request = new NotificationRequest(NOTIFICATION_ID); 
  49.         request.setAlertOneTime(true); 
  50.         NotificationRequest.NotificationNormalContent normalContent = new NotificationRequest.NotificationNormalContent(); 
  51.         normalContent.setText(DateUtils.getCurrentDate("yyyy-MM-dd HH:mm:ss")); 
  52.         NotificationRequest.NotificationContent content = new NotificationRequest.NotificationContent(normalContent); 
  53.         request.setContent(content); 
  54.         keepBackgroundRunning(NOTIFICATION_ID,request); 
  55.     } 
  56.  
  57.  
  58.     @Override 
  59.     protected void onBackground() { 
  60.         HiLog.info(TAG, "onBackground"); 
  61.         super.onBackground(); 
  62.     } 
  63.  
  64.     @Override 
  65.     protected void onStop() { 
  66.         HiLog.info(TAG, "onStop"); 
  67.         super.onStop(); 
  68.     } 

FormDatabase.java:

  1. @Database(entities = {FormBean.class}, version = 1) 
  2. public abstract class FormDatabase extends OrmDatabase { 

 ComponentProviderUtils.java:

  1. public class ComponentProviderUtils { 
  2.     private static Context mContext; 
  3.     private static final int DIMENSION_1X2 = 1; 
  4.     private static final int DIMENSION_2X2 = 2; 
  5.     private static final int DIMENSION_2X4 = 3; 
  6.     private static final int DIMENSION_4X4 = 4; 
  7.  
  8.     /** 
  9.      * 获取ComponentProvider 
  10.      * 
  11.      * @param formBean 
  12.      * @param context 
  13.      * @return ComponentProvider 
  14.      */ 
  15.     public static ComponentProvider getComponentProvider(FormBean formBean, Context context){ 
  16.         mContext = context; 
  17.         int layoutId = ResourceTable.Layout_form_image_with_info_time_2_2; 
  18.         if (formBean.getDimension() == DIMENSION_2X4){ 
  19.             layoutId = ResourceTable.Layout_form_image_with_info_time_2_4; 
  20.         }else if (formBean.getDimension() == DIMENSION_1X2){ 
  21.             layoutId = ResourceTable.Layout_form_image_with_info_time_1_2; 
  22.         } 
  23.         ComponentProvider componentProvider =new ComponentProvider(layoutId,context); 
  24.         setComponentProvider(componentProvider); 
  25.         return componentProvider; 
  26.     } 
  27.  
  28.     /** 
  29.      * 设置信息 
  30.      * 
  31.      * @param componentProvider 
  32.      */ 
  33.     private static void setComponentProvider(ComponentProvider componentProvider) { 
  34.         Calendar instance = Calendar.getInstance(); 
  35.         // 时分秒 
  36.         int hour = instance.get(Calendar.HOUR_OF_DAY); 
  37.         int min = instance.get(Calendar.MINUTE); 
  38.         int second = instance.get(Calendar.SECOND); 
  39.         componentProvider.setText(ResourceTable.Id_hour,intToString(hour)); 
  40.         componentProvider.setText(ResourceTable.Id_min,intToString(min)); 
  41.         componentProvider.setText(ResourceTable.Id_second,intToString(second)); 
  42.  
  43.         // 星期 
  44.         int week = instance.get(Calendar.DAY_OF_WEEK); 
  45.         String weekString = getWeek(week); 
  46.         componentProvider.setText(ResourceTable.Id_week,weekString); 
  47.  
  48.         // 日期 
  49.         String currentDate = DateUtils.getCurrentDate("yyyy-MM-dd"); 
  50.         String[] split = currentDate.split("-"); 
  51.         componentProvider.setText(ResourceTable.Id_month,split[1]); 
  52.         componentProvider.setText(ResourceTable.Id_day,split[2]); 
  53.  
  54.     } 
  55.  
  56.     public static String intToString(int time) { 
  57.         if (String.valueOf(time).length() < 2){ 
  58.             return "0"+time
  59.         }else { 
  60.             return time+""
  61.         } 
  62.     } 
  63.  
  64.     //获取星期 
  65.     public static String getWeek(int week) { 
  66.  
  67.         int stringId; 
  68.         switch (week) { 
  69.             case 1: 
  70.                 stringId = ResourceTable.String_SUNDAY; 
  71.                 break; 
  72.             case 2: 
  73.                 stringId = ResourceTable.String_MONDAY; 
  74.                 break; 
  75.             case 3: 
  76.                 stringId = ResourceTable.String_TUESDAY; 
  77.                 break; 
  78.             case 4: 
  79.                 stringId = ResourceTable.String_WEDNESDAY; 
  80.                 break; 
  81.             case 5: 
  82.                 stringId = ResourceTable.String_THURSDAY; 
  83.                 break; 
  84.             case 6: 
  85.                 stringId = ResourceTable.String_FRIDAY; 
  86.                 break; 
  87.             case 7: 
  88.                 stringId = ResourceTable.String_SATURDAY; 
  89.                 break; 
  90.             default
  91.                 stringId = ResourceTable.String_SUNDAY; 
  92.                 break; 
  93.         } 
  94.         return mContext.getString(stringId); 
  95.     } 

DatabaseUtils.java:

  1. public class DatabaseUtils { 
  2.  
  3.     /** 
  4.      * 添加卡片信息到数据库 
  5.      * 
  6.      * @param formBean 
  7.      * @param ormContext 
  8.      */ 
  9.     public static void insertForm(FormBean formBean, OrmContext ormContext){ 
  10.         ormContext.insert(formBean); 
  11.         ormContext.flush(); 
  12.     } 
  13.  
  14.     /** 
  15.      * 删除卡片信息 
  16.      * 
  17.      * @param formId 
  18.      * @param ormContext 
  19.      */ 
  20.     public static void deleteFormData(long formId,OrmContext ormContext){ 
  21.         OrmPredicates where = ormContext.where(FormBean.class); 
  22.         where.equalTo("formId",formId); 
  23.         List<FormBean> query = ormContext.query(where); 
  24.         if (!query.isEmpty()){ 
  25.             ormContext.delete(query.get(0)); 
  26.             ormContext.flush(); 
  27.         } 
  28.  
  29.     } 

DateUtils.java:

  1. public class DateUtils { 
  2.  
  3.     /** 
  4.      * 获取当前日期 
  5.      * 
  6.      * @param format format 
  7.      * @return 日期 
  8.      */ 
  9.     public static String getCurrentDate(String format) { 
  10.         DateFormat dateFormat = new SimpleDateFormat(format); 
  11.         Date date = new Date(); 
  12.         String formatDate = dateFormat.format(date); 
  13.         return formatDate; 
  14.     } 
  15.  
  16.     //获取星期 
  17.     public static String getWeek(Context context, int week) { 
  18.  
  19.         int stringId; 
  20.         switch (week) { 
  21.             case 1: 
  22.                 stringId = ResourceTable.String_SUNDAY; 
  23.                 break; 
  24.             case 2: 
  25.                 stringId = ResourceTable.String_MONDAY; 
  26.                 break; 
  27.             case 3: 
  28.                 stringId = ResourceTable.String_TUESDAY; 
  29.                 break; 
  30.             case 4: 
  31.                 stringId = ResourceTable.String_WEDNESDAY; 
  32.                 break; 
  33.             case 5: 
  34.                 stringId = ResourceTable.String_THURSDAY; 
  35.                 break; 
  36.             case 6: 
  37.                 stringId = ResourceTable.String_FRIDAY; 
  38.                 break; 
  39.             case 7: 
  40.                 stringId = ResourceTable.String_SATURDAY; 
  41.                 break; 
  42.             default
  43.                 stringId = ResourceTable.String_SUNDAY; 
  44.                 break; 
  45.         } 
  46.         return context.getString(stringId); 
  47.     } 

git地址:https://gitee.com/fzyme_admin/time-card.git

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-08-23 10:12:41

鸿蒙HarmonyOS应用

2021-06-21 15:21:52

鸿蒙HarmonyOS应用开发

2021-07-28 14:59:08

鸿蒙HarmonyOS应用

2023-08-04 17:24:43

2021-09-15 10:19:15

鸿蒙HarmonyOS应用

2021-09-18 09:57:20

鸿蒙HarmonyOS应用

2022-04-24 15:26:38

服务卡鸿蒙

2021-07-22 08:45:47

鸿蒙HarmonyOS应用

2023-09-13 15:27:14

鸿蒙应用元服务

2021-06-23 16:05:05

鸿蒙HarmonyOS应用

2023-04-07 09:20:55

2022-05-09 11:52:38

Java卡片服务卡片

2021-08-16 14:42:28

鸿蒙HarmonyOS应用

2021-08-20 14:23:14

鸿蒙HarmonyOS应用

2021-07-20 09:58:16

鸿蒙HarmonyOS应用

2023-08-07 14:09:58

数据库开发

2023-08-07 12:53:05

开发服务

2023-08-11 14:00:42

鸿蒙元服务

2010-05-25 11:35:02

JavaKilim

2021-06-24 14:41:16

鸿蒙HarmonyOS应用
点赞
收藏

51CTO技术栈公众号