动漫七夕服务卡片 原创 精华

狼哥Army
发布于 2021-8-16 23:20
浏览
4收藏

动漫七夕服务卡片, 动漫图片有香香小朋友提供原稿图片, 通过请求通知对方查看服务卡片, 发送祝贺到服务卡片上显示,表达自己的心里话.
知识点:
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"));
    }
}
  1. 分布式代码
    配置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();
    }
}
  1. 对象关系映射数据库
    要使@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 {
}
  1. 通知
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

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2021-8-21 22:51:20修改
6
收藏 4
回复
举报
2条回复
按时间正序
/
按时间倒序
mb609898e2cfb86
mb609898e2cfb86

小朋友图画的可以的

回复
2021-8-17 11:52:03
狼哥Army
狼哥Army 回复了 mb609898e2cfb86
小朋友图画的可以的

谢谢,今年才开始画动漫的,之前画儿童画。

回复
2021-8-17 11:54:18
回复
    相关推荐