动漫七夕服务卡片
动漫七夕服务卡片, 动漫图片有香香小朋友提供原稿图片, 通过请求通知对方查看服务卡片, 发送祝贺到服务卡片上显示,表达自己的心里话.
知识点:
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未知归属地
全部评论
最多点赞
最新发布
最早发布
写回答
所见即所得编辑器, editor1
工具栏
0/50000
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要发帖子
了解社区公约,与您携手共创和谐专业的开发者社区。







































苏公安网备 32011402010933号