客户端接口 - (v1)invoke

invoke

当小程序中调用 wmpf.Channel.invoke 时,Client 端需要使用 insert 操作完成异步调用。

传入参数

通过 ContentValues 传递

参数 类型 必填 说明
invoke_id String 调用 id ,用于后续获取对应的返回数据,每次调用的 id 都不一样
command String 小程序经 wmpf 透传给第三方 app 的指令名
data String 小程序经 wmpf 透传给第三方 app 的指令参数

返回参数

通过 insert 操作通知及 ContentValues 传递

参数 类型 必填 说明
invoke_id String 调用 id ,作为通知回调的凭据
command String 小程序经 wmpf 透传给第三方 app 的指令名
data String 小程序经 wmpf 透传给第三方 app 的指令参数

示例代码

override fun insert(p0: Uri, p1: ContentValues?): Uri? {
  when(sURIMatcher.match(p0)) {
    // 异步方法调用
    CODE_CALLBACK_INVOKE_CHANNEL -> {
      val invokeId = p1?.getAsString(InvokeChannelConstants.Key.INVOKE_ID)
      val command = p1?.getAsString(InvokeChannelConstants.Key.COMMAND)
      val sourceData = p1?.getAsString(InvokeChannelConstants.Key.DATA)

      Log.i(TAG, "invokeId: $invokeId, command: $command, sourceData: $sourceData")

      if (command.equals("test")) {
        // 实现特定的功能逻辑
        mHandler.post({
          val cv = ContentValues()
          cv.apply {
            put(InvokeChannelConstants.Key.INVOKE_ID, invokeId)
            put(InvokeChannelConstants.Key.COMMAND, command)
            put(InvokeChannelConstants.Key.DATA, "your data") // 返回执行结果给小程序端
          }
          try {
            context?.contentResolver?.insert(InvokeChannelConstants.ContentProvider.Cli2WMPF.URI_CALLBACK_INVOKE_CHANNEL, cv)
          } catch (e: Exception) {
            Log.e(TAG, "callback invoke channel error")
          }
        })
      }
    }
  }
  return null
}
posted on 2025-02-24 15:48  AtlasLapetos  阅读(26)  评论(0)    收藏  举报