DeepShare Android 集成

优质
小牛编辑
113浏览
2023-12-01

1. 配置Manifest文件

在应用入口Activity中增加DeepShare的intent-filter,这样APP就可以通过浏览器被唤起。

<application>
   <activity>
      <intent-filter>
         <data
             android:host="此处填写DeepShare配置中显示的host"
             android:scheme="此处填写DeepShare配置中显示的scheme" />
         <action android:name="android.intent.action.VIEW" />
         <category android:name="android.intent.category.DEFAULT" />
         <category android:name="android.intent.category.BROWSABLE" />
       </intent-filter>
   </activity>
</application>

2. 启动DeepShare

在应用入口Activity的onStart()中调用如下方法:

ZhugeParam param = new ZhugeParam.Builder()
                                        .appKey("appKey")
                                        .appChannel("channel")
                                        .inAppDataListener(listener)
                                        .did("did")
                                        .build();

ZhugeSDK.getInstance().initWithDeepShareAndParam(activity ,param);

initWithDeepShareAndParam() 初始化SDK和DeepShare功能,并配置启动参数。

参数说明:

参数说明
activity应用入口的activity
appKey官网申请的AppKey
channel应用分发渠道
listener用于获取场景还原参数的listener
did自定义设备ID,长度不超过256个字符

注意:

  1. init()initWithDeepShareAndParam()只能调用一个,同时调用会以第一个成功执行的为准。
  2. 调用initWithDeepShareAndParam()时,若ZhugeParam中没有配置appKey及appChannel,则会从应用的manifest文件中获取,若找不到appKey及appChannel则初始化失败。

3. 添加启动回调

在启动Activity的onNewIntent处添加如下代码:

@Override
public void onNewIntent(Intent intent) {
    this.setIntent(intent);
}

4. 获取场景还原参数

为实现场景还原功能,你需要实现ZhugeInAppDataListener 接口。

ZhugeInAppDataListener接口定义了场景调用成功时的回调函数zgOnInappDataReturned,及失败时的回调函数zgOnFailed。

在初始化SDK时,传入ZhugeInAppDataListener来获取场景还原参数,并通过重写异步回调方法zgOnInappDataReturned()获取分享参数params。

示例代码如下:

public class MainActivity extends Activity {
    public void onStart() {
        super.onStart();
        ZhugeParam param = new ZhugeParam.Builder()
                                .inAppDataListener(new MyListener())
                                .build();
       ZhugeSDK.getInstance().initWithDeepShareAndParam(this,param);
    }

    public static class MyListener implements ZhugeInAppDataListener{

        @Override
            public void zgOnInAppDataReturned(JSONObject initParams) {
         Log.e(TAG,"zgOnInAppDataReturned "+initParams.toString());
        }

        @Override
        public void zgOnFailed(String reason) {

        }
    }

}