文档开发者中心
我的

动漫七夕服务卡片

动漫七夕服务卡片, 动漫图片有香香小朋友提供原稿图片, 通过请求通知对方查看服务卡片, 发送祝贺到服务卡片上显示,表达自己的心里话.

知识点:     

    1. 服务卡片

    2. 分布式

    3. 对象关系映射数据库

    4. 通知

视频: 视频效果

1. 服务卡片代码

收起
自动换行
深色代码主题
复制

private void updateForms(String firstParam, String secondParam) { // 从数据库中获取卡片信息 OrmPredicates ormPredicates = new OrmPredicates(Form.class); List<Form> formList = connect.query(ormPredicates); // 更新卡片文字 if (formList.size() <= 0) { return; } ZSONObject zsonObject = new ZSONObject(); zsonObject.put("fourText", firstParam); zsonObject.put("eightText", secondParam); for (Form form : formList) { // 遍历卡片列表更新卡片 ComponentProvider componentProvider = ComponentProviderUtils.getComponentProvider(form, this, zsonObject); try { Long updateFormId = form.getFormId(); updateForm(updateFormId, componentProvider); } catch (FormException e) { // 删除不存在的卡片 DatabaseUtils.deleteFormData(form.getFormId(), connect); System.out.println("[updateForms]onUpdateForm updateForm error"); } }}public static ComponentProvider getComponentProvider(Form form, Context context, ZSONObject zsonObject) { // 默认为2X2规格布置 int layoutId = ResourceTable.Layout_form_immersive_pattern_widget_2_2; if (form.getDimension() == DIMENSION_2X4) { // 设置规格布置为2X4 layoutId = ResourceTable.Layout_form_immersive_pattern_widget_2_4; }else if (form.getDimension() == DIMENSION_4X4) { // 设置规格布置为4X4 layoutId = ResourceTable.Layout_form_immersive_pattern_widget_4_4; }else if (form.getDimension() == DIMENSION_1X2) { // 设置规格布置为1X2 layoutId = ResourceTable.Layout_form_immersive_pattern_widget_1_2; } ComponentProvider componentProvider = new ComponentProvider(layoutId, context); // 更新卡片组件内容 setComponentProviderValue(form, componentProvider, zsonObject); return componentProvider;}private static void setComponentProviderValue(Form form, ComponentProvider componentProvider, ZSONObject zsonObject) { // zsonObject为要更新的文本对象 // 更新参数里显示四个字祝贺字的文本组件 componentProvider.setText(ResourceTable.Id_fourCard, zsonObject.getString("fourText")); if (form.getDimension() != DIMENSION_1X2) { // 更新参数里显示八个字祝贺字的文本组件 componentProvider.setText(ResourceTable.Id_eightCard, zsonObject.getString("eightText")); }}

2. 分布式代码

配置config.json权限申请

收起
自动换行
深色代码主题
复制

"reqPermissions": [   {      "name": "ohos.permission.GET_DISTRIBUTED_DEVICE_INFO"   },   {      "name": "ohos.permission.DISTRIBUTED_DATASYNC"   }]

Java权限申请

收起
自动换行
深色代码主题
复制

private void requestPermissions() {    String[] permissions = {SystemPermission.DISTRIBUTED_DATASYNC};    List<String> permissionsToProcess = new ArrayList<>();    for (String permission : permissions) {        if (verifySelfPermission(permission) != 0 && canRequestPermission(permission)) {            permissionsToProcess.add(permission);        }    }    requestPermissionsFromUser(permissionsToProcess.toArray(new String[0]), 0);}
收起
自动换行
深色代码主题
复制

public class MyRemoteObject extends RemoteObject implements IRemoteBroker {    public MyRemoteObject(String descriptor) {        super(descriptor);    }    @Override    public IRemoteObject asObject() {        return this;    }    @Override    public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {        String fourText = data.readString();        String eightText = data.readString();        // 更新卡片信息        updateForms(fourText, eightText);        // 调用通知提示        notice();        return true;   }    @Override    protected IRemoteObject onConnect(Intent intent) {        System.out.println("SendServiceAbility::onConnect");        return new MyRemoteObject("myRemoteObject").asObject();    }}

3. 对象关系映射数据库

要使@Entity和@Database可以导入相应类, 记得在config.json文件加上以下配置

收起
自动换行
深色代码主题
复制

ohos {    compileOptions { annotationEnabled true }}
收起
自动换行
深色代码主题
复制

@Entity(tableName = "form")public class Form extends OrmObject {    @PrimaryKey()    private Long formId;        // 卡片ID  private String formName;    // 卡片名称    private Integer dimension;  // 卡片规格    public Form(Long formId, String formName, Integer dimension) {        this.formId = formId;        this.formName = formName;        this.dimension = dimension;    }    // 省略Get和Set方法....}
收起
自动换行
深色代码主题
复制

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

4. 通知

收起
自动换行
深色代码主题
复制

private void notice() {    // 创建通知    NotificationRequest request = new NotificationRequest(NOTICE_ID);    request.setAlertOneTime(true);    NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();    content.setText("动漫七夕服务卡片有更新,请查看");    NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);    request.setContent(notificationContent);    // 绑定通知    keepBackgroundRunning(NOTICE_ID, request);}

源代码: https://gitee.com/army16_harmony/comic-qixi

点赞
收藏
1
回复
4
分享
举报
浏览990 编辑于2021-08-21 15:00未知归属地
全部评论
最多点赞
最新发布
最早发布
保存了,这玩意儿通用了哈哈哈
1
2楼回复于2021-08-17 08:20 未知归属地
  • 狼哥
    是的,可以下载源码在这基础上发挥想象力,弄个更好玩的卡片
    2021-08-17 09:29 未知归属地
Q版可爱,改一改或许中秋也可
1
1楼回复于2021-08-17 07:08 未知归属地
  • 狼哥
    是的, 每逢佳节感动系列卡片之一
    2021-08-17 07:52 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要发帖子

了解社区公约,与您携手共创和谐专业的开发者社区。