supersdk

文档中心

文档中心

下载文档

H5SDK module integration document


The game can use the H5SDK module to implement custom announcements and add activities quickly.

Preparation before integration

The game developer needs to find the SuperSDK colleagues to open the function.

Engineering configuration


Version

According to the needs of the game, android:minSdkVersion and android:targetSdkVersion must be set.

Suggested game activity to add android: alwaysRetainTaskState = “true”

Add Permission

Add the following permissions to the manifest file

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.CAMERA"/>

Manifest file registration

Add the following Activity to the manifest file

<activity
    android:name="com.youzu.h5sdklib.PlayerActivity.MyPlayer"
    android:configChanges="orientation|screenSize|keyboardHidden|keyboard"
    android:screenOrientation="sensorPortrait"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>
<activity
    android:name="com.youzu.h5sdklib.imagepicker.ui.activity.ImagePreviewActivity"
    android:theme="@android:style/Theme.NoTitleBar">
</activity>

<activity
    android:name="com.youzu.h5sdklib.imagepicker.ui.activity.ImagesGridActivity"
    android:theme="@android:style/Theme.NoTitleBar">
</activity>

<activity
    android:name="com.youzu.h5sdklib.imagepicker.ui.activity.ImageCropActivity"
    android:theme="@android:style/Theme.NoTitleBar">
</activity>
<activity
    android:name="com.youzu.h5sdklib.WebActivity.PermissionActivity"
    android:configChanges="orientation|screenSize|keyboardHidden|keyboard"
    android:screenOrientation="sensor"
    android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>

Interface Description


Open the H5 page interface

Note 1: When the game opens the H5 page, you can choose to turn off the game sound in order to prevent audio/video conflicts with the H5 page. In addition, when receives the callback of the H5 page close, then turn on the game sound.

Note 2: Among them, ShareSdk has two parameters: mobAppKey, mobAppSecret: because the 3.0+ version of sharesdk is initialized differently than the 2.0+ version of sharesdk.

If you are integrating the 2.0+ version of sharesdk, these two parameters may not be passed. If it is a 3.0+ version of sharesdk, these two parameters must be passed and cannot be empty strings, otherwise, the sharesdk initialization fails and cannot run the sharing function.

Map<String, Object> params = new HashMap<String, Object>();

params.put("h5url", "https://sdkcgi.youzu.com/others/h5SdkDemo");//  H5页面测试地址、可测试绝大部分H5SDK功能
params.put("ticket","eyJvc2RrX2dhbWVfaWQiOiIxOTYzNzc0MjYiLCJ1c2VyX2lkIjoidTE4MjI2NzYxNzI1IiwibG9naW5fc2RrX25hbWUiOiJ5b3V6dSIsImNoYW5uZWxfaWQiOiIwIiwiZXh0ZW5kIjoiMDAxOTAwMXwxfDAwMTkwMDEiLCJhY2NvdW50X3N5c3RlbV9pZCI6IjAwNjAwMTUiLCJvc2RrX3VzZXJfaWQiOiIwMDYwMDE1X3UxODIyNjc2MTcyNSIsImlwIjoiMTAxLjIyNy4xMDIuNCIsImNvdW50cnkiOiJDTiIsInRpbWUiOjE1MTIzNzQ3MjksInNpZ24iOiI4ZGYzYWY5ODNiZmVmZmZjMWExZDhmM2VlZGZkYmE2YiJ9");// SuperSDK用户登录票据,可用于用户验证
params.put("server_id", GameParamsHandler.getInstance().getmServerId());//游戏区服编号
params.put("server_name", GameParamsHandler.getInstance().getmServerName());//游戏区服名称
params.put("role_id", GameParamsHandler.getInstance().getmRoleId());//角色编号
params.put("role_name", GameParamsHandler.getInstance().getmRoleName());// 角色名
params.put("level", GameParamsHandler.getInstance().getmRoleLevel());// 角色等级
params.put("vip_grade", GameParamsHandler.getInstance().getmRoleVip());//VIP等级
params.put("opid", opId);//渠道编号
params.put("extend", "{}");// 扩展参数,可不传
params.put("mobAppKey", "mobAppKey");//ShareSDK参数
params.put("mobAppSecret", "mobAppKey");//ShareSDK参数

SuperSDK.invoke("h5sdk", "openH5View", map);

H5 payment protocol callback

H5SDK invokes the original payment, through this callback, H5SDK transmits the product number to the original side, and the orginal side implements the payment function. Because H5 only runs the callback after the order number is obtained, it is not necessary to judge the code of the callback.

private OnSuperSDKListener mSuperSDKListener = new OnSuperSDKListener() {

       @Override
       public void onSuperSDK(String moduleName, String funcName, String result) {
           if ("h5sdk".equals(moduleName) && "callNativeAPIForPay".equals(funcName)) {
               try {
                   JSONObject payJson = new JSONObject(result);
                   String productId  = payJson .getString("data"); 
                   //进行支付处理                      
               } catch (JSONException e) {
                   e.printStackTrace();
               }
           }
       }
   };

Callback log printing:

D/bcore: com.youzu.bcore.module.h5.H5SDKHandler$2.paymentBlock(L:160): callNativeAPIForPay|返回的结果为:{"code":"1","msg":"h5调用原生支付功能,需要原生端收到回调后实现支付功能","data":"1"}

Pass the order number to the H5SDK interface

After the payment agreement is implemented, when the payment agreement initiates payment, the order number of the request is returned to the H5SDK through this interface. For example, using the payment code of SuperSDK itself, you only need to listen to the callback that successfully obtaining the order number, and invoke this method in the callback.

private OnSuperSDKListener mSuperSDKListener = new OnSuperSDKListener() {

       @Override
       public void onSuperSDK(String moduleName, String funcName, String result) {
           if (BCoreConst.platform.MODULE_NAME.equals(moduleName)&& BCoreConst.platform.FUNC_PAY_ORDER_ID.equals(funcName)) {
                JSONObject json = JsonUtils.parseObject(result);
                // 获取订单成功
                String orderId = json.getString("data");
                BCoreLog.e("payOrderId success: " + orderId);
                sendMsg(MainActivity.ORDER_ID, orderId);
                GameParamsHandler.getInstance().setOrderId(orderId);
                BCoreLog.e("获取订单成功: " + orderId);

                Map<String, Object> params = new HashMap<String, Object>();
                params.put("orderId", orderId);
                SuperSDK.invoke("h5sdk", "callH5APIForOderID", params);
           }
       }
   };

H5 general protocol callback

H5SDK invokes the orginal interface, through this callback, H5SDK passes the parameters to the orginal side, and the orginal side implements some protocol function, so it is better to use hot update to implement the function.

private OnSuperSDKListener mSuperSDKListener = new OnSuperSDKListener() {

       @Override
       public void onSuperSDK(String moduleName, String funcName, String result) {
           if ("h5sdk".equals(moduleName) && "callNativeAPIForCommon".equals(funcName)) {
               try {
                   JSONObject commonJson = new JSONObject(result); 
                   String data = commonJson.getString("data");
                   JsonObject param = new JsonObject(data);
                   //这里进行需要自己实现的功能                    
               } catch (JSONException e) {
                   e.printStackTrace();
               }
           }
       }
   };

Callback log printing:

D/bcore: com.youzu.bcore.module.h5.H5SDKHandler$3.commonProtocol(L:196): callNativeAPIForCommon|返回的结果为:{"code":"1","msg":"h5调用原生通用接口,需要原生端收到回调后实现相应功能","data":"{\"key0\":\"测试一\",\"key1\":1,\"key2\":{\"key20\":\"哈哈\",\"key21\":33},\"key3\":\"测试三\"}"}

Orginal invoke to H5 general interface

The H5 general interface is invoked by orginal. It is recommended to invoke it when the H5 page is opened and completely loaded. Generally, it is used with the H5 general protocol callback.

private OnSuperSDKListener mSuperSDKListener = new OnSuperSDKListener() {

       @Override
       public void onSuperSDK(String moduleName, String funcName, String result) {
           if ("h5sdk".equals(moduleName) && "callNativeAPIForCommon".equals(funcName)) {
               try {
                   JSONObject commonJson = new JSONObject(result); 
                   String data = commonJson.getString("data");
                   JsonObject param = new JsonObject(data);
                   //这里进行需要自己实现的功能 
                   //处理完成后将结果传递给H5
                   Map<String, Object> params = new HashMap<String, Object>();
                   params.put("key1", value1);
                   params.put("key2", value2);
                   params.put("key3", value3);
                   SuperSDK.invoke("h5sdk", "callH5APIForCommon", params);                   
               } catch (JSONException e) {
                   e.printStackTrace();
               }
           }
       }
   };

H5 page close callback

The h5 page is closed, if the game sound is turned off when the h5 page is opened, please restore the game sound after receiving this callback.

private OnSuperSDKListener mSuperSDKListener = new OnSuperSDKListener() {

       @Override
       public void onSuperSDK(String moduleName, String funcName, String result) {
           if ("h5sdk".equals(moduleName) && "H5ViewDidClose".equals(funcName)) {
               //如果游戏在打开H5SDK的时候选择关闭了游戏声音,可以在这里选择打开游戏声音
           }
       }
   };

Callback log printing:

E/bcore: com.supersdk.module.demo.GameReultHandler$1.onSuperSDK(L:63): moduleName:h5sdk,funcName:H5ViewDidClose,result:{"code":"1","msg":"h5页面关闭,如果在打开h5页面时,关闭了游戏声音,请在收到此回调后恢复游戏声音"}