HarmonyOS之基于华为分享的服务分享接入
【摘要】
一、简介
① 业务流程
原子化服务所提供的便捷服务,可以通过接入华为分享实现近距离快速分享,使便捷服务可以精准快速的推送至接收方,降低用户触达服务的成本,提升用户体验。相比于传统的社交软件分享,分享双...
一、简介
① 业务流程
- 原子化服务所提供的便捷服务,可以通过接入华为分享实现近距离快速分享,使便捷服务可以精准快速的推送至接收方,降低用户触达服务的成本,提升用户体验。
- 相比于传统的社交软件分享,分享双方无需建立好友关系,接收方无需提前安装承载服务的安装包,即可享受原生的服务体验。
- 基于华为分享的服务分享用户业务流程示例:
-
- 分享方选择要分享的服务;
-
- 选择“华为分享”作为分享途径;
-
- 选择分享的接收方;
-
- 接收方直接打开服务或在用户确认后打开服务:
-
-
- 当分享方和接收方是同帐号时,直接打开服务详情界面;
-
-
-
- 当分享方和接收方是无帐号或跨帐号时,接收方弹出卡片简介,用户点击卡片中的“打开”按钮后,进入服务详情界面。
-
- 基于华为分享的服务分享流程图:
② 运作机制
- 基于华为分享的服务分享工作原理如下图所示:
- 服务分享原理说明:
-
- 如果分享方与接收方的华为帐号相同,则分享后会在接收方直接为用户打开服务。
-
- 如果分享方与接收方无帐号或跨帐号,则分享后会首先在接收方弹出卡片为用户简述分享内容,用户确认后才会打开服务。
二、服务接入华为分享步骤
- 集成 IDL 接口,用于建立分享方与华为分享的交互通道,完成后续服务分享过程。
在“java”目录同级目录创建“idl”接口目录(可手动添加或通过 DevEco Studio 创建):com/huawei/hwshare/third(固定路径),然后创建名为 IHwShareCallback.idl 和 IHwShareService.idl 的 IDL 文件。
IHwShareCallback.idl :
interface com.huawei.hwshare.third.IHwShareCallback {
[oneway] void notifyState([in] int state);
}
IHwShareService.idl :
sequenceable ohos.interwork.utils.PacMapEx;
interface com.huawei.hwshare.third.IHwShareCallback;
interface com.huawei.hwshare.third.IHwShareService {
int startAuth([in] String appId, [in] IHwShareCallback callback);
int shareFaInfo([in] PacMapEx pacMapEx);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 创建 ShareFaManager 类,用于管理分享方与华为分享的连接通道和数据交互:
import com.huawei.hwshare.third.HwShareCallbackStub;
import com.huawei.hwshare.third.HwShareServiceProxy;
import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.bundle.ElementName;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.interwork.utils.PacMapEx;
import ohos.rpc.IRemoteObject;
import ohos.rpc.RemoteException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
public class ShareFaManager {
private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, "ShareFa");
private static final String LOG_FORMAT = "%{public}s: %{public}s";
public static final String HM_FA_ICON = "ohos_fa_icon";
public static final String HM_FA_NAME = "ohos_fa_name";
public static final String HM_ABILITY_NAME = "ohos_ability_name";
public static final String HM_BUNDLE_NAME = "ohos_bundle_name";
public static final String SHARING_FA_TYPE = "sharing_fa_type";
public static final String SHARING_THUMB_DATA = "sharing_fa_thumb_data";
public static final String SHARING_CONTENT_INFO = "sharing_fa_content_info";
public static final String SHARING_EXTRA_INFO = "sharing_fa_extra_info";
private static final String TAG = "ShareHmFaManager";
private static final String SHARE_PKG_NAME = "com.huawei.android.instantshare";
private static final String SHARE_ACTION = "com.huawei.instantshare.action.THIRD_SHARE";
private static final long UNBIND_TIME = 20*1000L;
private Context mContext;
private String mAppId;
private PacMapEx mSharePacMap;
private static ShareFaManager sSingleInstance;
private HwShareServiceProxy mShareService;
private boolean mHasPermission = false;
private EventHandler mHandler = new EventHandler(EventRunner.getMainEventRunner());
private final IAbilityConnection mConnection = new IAbilityConnection() {
@Override
public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int i) {
HiLog.error(LABEL_LOG, LOG_FORMAT, TAG, "onAbilityConnectDone success.");
mHandler.postTask(()->{
mShareService = new HwShareServiceProxy(iRemoteObject);
try {
mShareService.startAuth(mAppId, mFaCallback);
} catch (RemoteException e) {
HiLog.error(LABEL_LOG, LOG_FORMAT, TAG, "startAuth error.");
}
});
}
@Override
public void onAbilityDisconnectDone(ElementName elementName, int i) {
HiLog.info(LABEL_LOG, LOG_FORMAT, TAG, "onAbilityDisconnectDone.");
mHandler.postTask(()->{
mShareService = null;
mHasPermission = false;
});
}
};
private Runnable mTask = () -> {
if (mContext != null && mShareService != null) {
mContext.disconnectAbility(mConnection);
mHasPermission = false;
mShareService = null;
}
};
private final HwShareCallbackStub mFaCallback = new HwShareCallbackStub("HwShareCallbackStub") {
@Override
public void notifyState(int state) throws RemoteException {
mHandler.postTask(()->{
HiLog.info(LABEL_LOG, LOG_FORMAT, TAG, "notifyState: " + state);
if (state == 0) {
mHasPermission = true;
if (mSharePacMap != null) {
shareFaInfo();
}
}
});
}
};
/**
* 单例模式获取ShareFaManager的实例对象
*
* @param context 程序Context
* @return ShareFaManager实例对象
*/
public static synchronized ShareFaManager getInstance(Context context) {
if (sSingleInstance == null && context != null) {
sSingleInstance = new ShareFaManager(context.getApplicationContext());
}
return sSingleInstance;
}
private ShareFaManager(Context context) {
mContext = context;
}
private void shareFaInfo() {
if (mShareService == null) {
return;
}
if (mHasPermission) {
HiLog.info(LABEL_LOG, LOG_FORMAT, TAG, "start shareFaInfo.");
try {
mShareService.shareFaInfo(mSharePacMap);
mSharePacMap = null;
} catch (RemoteException e) {
HiLog.error(LABEL_LOG, LOG_FORMAT, TAG, "shareFaInfo error.");
}
}
// 不使用时断开
mHandler.postTask(mTask, UNBIND_TIME);
}
/**
* 用于分享服务
*
* @param appId 开发者联盟网站创建原子化服务时生成的appid
* @param pacMap 服务信息载体
*/
public void shareFaInfo(String appId, PacMapEx pacMap) {
if (mContext == null) {
return;
}
mAppId = appId;
mSharePacMap = pacMap;
mHandler.removeTask(mTask);
shareFaInfo();
bindShareService();
}
private void bindShareService() {
if (mShareService != null) {
return;
}
HiLog.error(LABEL_LOG, LOG_FORMAT, TAG, "start bindShareService.");
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName(SHARE_PKG_NAME)
.withAction(SHARE_ACTION)
.withFlags(Intent.FLAG_NOT_OHOS_COMPONENT)
.build();
intent.setOperation(operation);
mContext.connectAbility(intent, mConnection);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 封装服务分享数据,调用 ShareFaManager 封装的接口完成服务的分享:
PacMapEx pacMap = new PacMapEx();
pacMap.putObjectValue(ShareFaManager.SHARING_FA_TYPE, 0);
pacMap.putObjectValue(ShareFaManager.HM_BUNDLE_NAME, getBundleName());
pacMap.putObjectValue(ShareFaManager.SHARING_EXTRA_INFO, "xxxxxxxx");
pacMap.putObjectValue(ShareFaManager.HM_ABILITY_NAME, XxxAbility.class.getName());
pacMap.putObjectValue(ShareFaManager.SHARING_CONTENT_INFO, "xxxxxxx");
pacMap.putObjectValue(ShareFaManager.SHARING_THUMB_DATA, picByte);
pacMap.putObjectValue(ShareFaManager.HM_FA_ICON, iconByte);
pacMap.putObjectValue(ShareFaManager.HM_FA_NAME, "FAShareDemo");
// 第一个参数为appid,在华为AGC创建原子化服务时自动生成
ShareFaManager.getInstance(MainAbilitySlice.this).shareFaInfo("xxxxxxxxxx", pacMap);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- ShareFaManager 的常量字段说明:
常量字段 | 类型 | 描述 |
---|---|---|
ShareFaManager.SHARING_FA_TYPE | int | 分享的服务类型,当前只支持默认值0,非必选参数。如果不传递此参数,则接收方默认赋值为0 |
ShareFaManager.HM_BUNDLE_NAME | String | 分享的服务的bundleName,最大长度1024,必选参数 |
ShareFaManager.SHARING_EXTRA_INFO | String | 携带的额外信息,可传递到被拉起的服务界面 ,最大长度10240,非必选参数 |
ShareFaManager.HM_ABILITY_NAME | String | 分享的服务的Ability类名,最大长度1024,必选参数 |
ShareFaManager.SHARING_CONTENT_INFO | String | 卡片展示的服务介绍信息,最大长度1024,必选参数 |
ShareFaManager.SHARING_THUMB_DATA | byte[] | 卡片展示服务介绍图片,最大长度153600,必选参数 |
ShareFaManager.HM_FA_ICON | byte[] | 服务图标,如果不传递此参数,取分享方默认服务图标,最大长度32768,非必选参数 |
ShareFaManager.HM_FA_NAME | String | 卡片展示的服务名称,最大长度1024,非必选参数。如果不传递此参数,取分享方默认服务名称 |
文章来源: blog.csdn.net,作者:Serendipity·y,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/Forever_wj/article/details/118725487
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)