HarmonyOS之基于华为分享的服务分享接入指南
Posted Forever_wj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
}
- 创建 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);
}
}
- 封装服务分享数据,调用 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,非必选参数。如果不传递此参数,取分享方默认服务名称 |
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);
以上是关于HarmonyOS之基于华为分享的服务分享接入指南的主要内容,如果未能解决你的问题,请参考以下文章
玩转华为ENSP模拟器系列 | 配置基于iBGP的PE和接入设备间路由交换