CaaS Engine是华为依托畅连业务通信能力,面向应用开发者和硬件开发者提供的开放接口,帮助开发者实现应用内实时共享的能力,助力用户即刻分享应用内允许共享的内容。

您将建立什么

在这个Codelab中,你将创建Demo Project,并将CaaS Engine的SDK集成到Demo Project中,以及完成CaaS Engine服务的整体流程搭建。

您将会学到什么

硬件要求

软件要求

需要的知识点

集成CaaS Engine能力,需要完成以下准备工作:

具体操作,请按照《开发指南》中"应用开发"的详细说明来完成。

1. 集成SDK。

向项目中的build.gradle中配置仓库:

buildscript { repositories { google() jcenter() maven {url 'http://developer.huawei.com/repo/'} } } allprojects { repositories { maven {url 'http://developer.huawei.com/repo/'} google() jcenter() } }

在工程APP目录中的build.gradle中添加如下依赖:

repositories { flatDir { dirs 'libs' } } dependencies { implementation group: 'com.huawei.caaskit', name: 'caaskitlite', version: '1.0.1.400', ext: 'aar' }

点击同步工程按钮,进行工程同步,如下图所示:

同步成功后,如下图所示:

2. 添加appid信息。

在自身应用AndroidManifest.xml中添加开发者联盟创建应用时生成的appid信息,name为"com.huawei.hms.client.appid",value为"appid=*",*用自己注册的appid。权限申请,请依照《开发指南》中"开发准备"的详细说明来完成。

3. 初始化。

CaaS服务的初始化封装代码如下:

CaasKitHelper.java

public class CaasKitHelper { private static final String TAG = "CaasKitHelper"; private static CaasKitHelper sCaasKitHelper; private HwCaasServiceManager mHwCaasServiceManager; private HwCaasHandler mHwCaasHandler; private Context mContext; private boolean mIsSendShowFail; private boolean mIsCaasKitInit; private boolean mIsHasCaaSContacts = false; private HwCallStateCallBack mCallStateCallBack = new HwCallStateCallBack() { @Override public void notifyCallState(HwCaasUtils.CallState state) { Log.d(TAG, "callState: " + state); } }; private HwCaasServiceCallBack mCallBack = new HwCaasServiceCallBack() { @Override public void initSuccess(HwCaasHandler handler) { ... } @Override public void initFail(int retCode) { ... } @Override public void releaseSuccess() { ... } }; private CaasKitHelper() { mContext = CaasKitApplication.getContext(); } public synchronized static CaasKitHelper getInstance() { if (sCaasKitHelper == null) { sCaasKitHelper = new CaasKitHelper(); } return sCaasKitHelper; } public void caasKitInit() { Log.d(TAG, "caasKitInit."); if (!mIsCaasKitInit) { // initialize mHwCaasServiceManager instance. mHwCaasServiceManager = HwCaasServiceManager.init(); // initialize HwCaasHandler instance through handlerType. mHwCaasServiceManager.initHandler(mContext, HwCaasUtils.SCREEN_SHARING_TYPE, mCallBack); mIsCaasKitInit = true; } } }

在合适的位置进行调用:

CaasKitDemoActivity.java

public class CaasKitDemoActivity extends AppCompatActivity { private static final String TAG = "CaaSKitDemoActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_caaskitdemo); CaasKitHelper.getInstance().caasKitInit(); } }

4. 设置联系人窗口显示样式。

CaasKitHelper.java

public class CaasKitHelper { private static final String TAG = "CaasKitHelper"; private static final int VIEWHEIGHT = 248; private static final int VIEWWIDTH = 256; private static final int LOCATION_X = 102; private static final int LOCATION_Y = 140; private static CaasKitHelper sCaasKitHelper; private HwCaasServiceManager mHwCaasServiceManager; private HwCaasHandler mHwCaasHandler; private Context mContext; private boolean mIsSendShowFail; private boolean mIsCaasKitInit; private boolean mIsHasCaaSContacts = false; private HwCallStateCallBack mCallStateCallBack = new HwCallStateCallBack() { @Override public void notifyCallState(HwCaasUtils.CallState state) { Log.d(TAG, "callState: " + state); } }; private HwCaasServiceCallBack mCallBack = new HwCaasServiceCallBack() { @Override public void initSuccess(HwCaasHandler handler) { // callback after successful initialization of HwCaasHandler. mHwCaasHandler = handler; if (mHwCaasHandler != null) { // 显示在对端呼入界面的来电应用名称,必须设置,不然畅连入口无法显示 mHwCaasHandler.setCallerAppName("Application"); // 联系人选择悬浮窗 也可以选择全屏列表(类似系统联系人界面) mHwCaasHandler.setContactViewStyle(HwCaasUtils.ContactsViewStyle.FULL_SCREEN); // 当选择悬浮窗时,可设置悬浮窗的大小和初始位置,选择全屏联系人界面了这些设置就不需要了 // mHwCaasHandler.setContactViewStyle(HwCaasUtils.ContactsViewStyle.FLOAT_VIEW); // mHwCaasHandler.setContactViewSize(VIEWWIDTH, VIEWHEIGHT); // mHwCaasHandler.setFloatViewLocation(HwCaasUtils.STARTVIEW, // HwCaasUtils.POINT_RIGHTANDDOWN, LOCATION_X, LOCATION_Y); // mHwCaasHandler.setFloatViewLocation(HwCaasUtils.CONTACTVIEW, HwCaasUtils.POINT_RIGHTANDDOWN, LOCATION_X, LOCATION_Y); } } @Override public void initFail(int retCode) { ... } @Override public void releaseSuccess() { ... } }; }

5. 实现屏幕共享。

CaasKitHelper.java

public class CaasKitHelper { private static final String TAG = "CaasKitHelper"; private HwCaasHandler mHwCaasHandler; public void pauseShare() { Log.d(TAG, "pauseShare."); if (mHwCaasHandler != null) { boolean isSendoK = mHwCaasHandler.sendEventToCaasService(HwCaasUtils.SCREEN_SHARING_PAUSE); Log.d(TAG, "isSendoK: " + isSendoK); } } public void resumeShare() { Log.d(TAG, "resumeShare."); if (mHwCaasHandler != null) { boolean isSendoK = mHwCaasHandler.sendEventToCaasService(HwCaasUtils.SCREEN_SHARING_RESUME); Log.d(TAG, "isSendoK: " + isSendoK); } } }

在合适的位置调用:

CaasKitDemoActivity.java

public class CaasKitDemoActivity extends AppCompatActivity { private static final String TAG = "CaaSKitDemoActivity"; private Button mSharePause; private Button mShareResume; @Override protected void onCreate(Bundle savedInstanceState) { ... mSharePause = findViewById(R.id.share_pause); mShareResume = findViewById(R.id.share_resume); mSharePause.setOnClickListener(v ->{ CaasKitHelper.getInstance().pauseShare(); }); mShareResume.setOnClickListener(v ->{ CaasKitHelper.getInstance().resumeShare(); }); } }

6. 显示悬浮通话界面。

CaasKitHelper.java

public class CaasKitHelper { private static final String TAG = "CaasKitHelper"; private static CaasKitHelper sCaasKitHelper; private HwCaasServiceManager mHwCaasServiceManager; private HwCaasHandler mHwCaasHandler; private Context mContext; private boolean mIsSendShowFail; private boolean mIsCaasKitInit; private boolean mIsHasCaaSContacts = false; public void sendShow() { Log.d(TAG, "sendShow."); if (mHwCaasHandler != null) { Log.d(TAG, "isHasCaaSContacts: " + mIsHasCaaSContacts); if (mIsHasCaaSContacts) { // 点击入口在应用侧,直接显示联系人列表 boolean ret = mHwCaasHandler.sendEventToCaasService(HwCaasUtils.SHOW_CONTACTS); // 应用侧控制显示悬浮球,用户点击后显示联系人列表 // boolean ret = mHwCaasHandler.sendEventToCaasService(HwCaasUtils.SHOW); Log.d(TAG, "ret: " + ret); } } else { mIsSendShowFail = true; Log.e(TAG, "sendShow fail."); } } public boolean sendHide() { Log.d(TAG, "sendHide."); if (mHwCaasHandler != null) { // 隐藏联系人列表 与 HwCaasUtils.SHOW_CONTACTS 配对使用 boolean ret = mHwCaasHandler.sendEventToCaasService(HwCaasUtils.HIDE_CONTACTS); // 隐藏悬浮球 与 HwCaasUtils.SHOW 配对使用 // boolean isSendoK = mHwCaasHandler.sendEventToCaasService(HwCaasUtils.HIDE); Log.d(TAG, "ret: " + ret); return ret; } Log.e(TAG, "sendHide fail."); return false; } }

在合适的位置调用:

CaasKitDemoActivity.java

public class CaasKitDemoActivity extends AppCompatActivity { private static final String TAG = "CaaSKitDemoActivity"; private Button mCallShow; private Button mCallHide; @Override protected void onCreate(Bundle savedInstanceState) { ... mCallShow = findViewById(R.id.call_show); mCallHide = findViewById(R.id.call_hide); mCallShow.setOnClickListener(v ->{ CaasKitHelper.getInstance().sendShow(); }); mCallHide.setOnClickListener(v ->{ CaasKitHelper.getInstance().sendHide(); }); } }

7. 释放资源。

CaasKitHelper.java

public class CaasKitHelper { ... public void caasKitRelease() { Log.d(TAG, "caasKitRelease." + mIsCaasKitInit); if (mIsCaasKitInit) { if (mHwCaasServiceManager != null) { // source release. mHwCaasServiceManager.release(); mHwCaasServiceManager = null; } mIsCaasKitInit = false; } } }

在合适的位置调用:

CaasKitDemoActivity.java

public class CaasKitDemoActivity extends AppCompatActivity { @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy."); CaasKitHelper.getInstance().caasKitRelease(); } }

祝贺你,你已经成功完成了Codelab并学到了:

您可以阅读下面链接,了解更多相关的信息。
相关文档

源码下载

已复制代码