API Reference
MiniAppPlugin
interface MiniAppService{
fun setup(
context: Context,
appName: String,
webAppName: String,
miniAppHost: List<String>,
languageCode: String = "en",
isDark: Boolean = false,
maxCachePage: Int = 5,
resourcesProvider: IResourcesProvider? = null,
appDelegate: IAppDelegate? = null
)
fun setThemeStyle(isDark: Boolean)
fun setLanguage(languageCode: String)
fun preload(config: WebAppPreloadParameter)
fun launch(config: WebAppLaunchParameter): IMiniApp
}
接口说明:
MiniAppPlugin
接口定义了与小程序客户端交互的方法。通过实现该接口,可以进行小程序的初始化、设置主题风格、设置语言、启动小程序等操作。
方法
-
setup(context: Context, appName: String, webAppName: String, miniAppHost: List<String>, languageCode: String = "en", isDark: Boolean = false, resourcesProvider: IResourcesProvider? = null, maxCachePage: Int = 5, appDelegate: IAppDelegate? = null)
初始化小程序客户端。
context
: Android 上下文对象。appName
: 小程序名称。- webAppName: WebApp命名空间
miniAppHost
: 小程序的主机列表。languageCode
: 小程序的语言代码,默认为 "en"。isDark
: 是否使用暗色主题,默认为false
。maxCachePage
: WebApp缓存最大数量,默认为 5resourcesProvider
: 资源提供者接口,用于自定义小程序的资源。appDelegate
: 应用委托接口,用于处理小程序的生命周期事件。
-
setThemeStyle(isDark: Boolean)
设置小程序的主题风格。
isDark
: 是否使用暗色主题。
-
setLanguage(languageCode: String)
设置小程序的语言。
languageCode
: 语言代码,例如 "en-US" 表示英语。
-
preload(config: WebAppPreloadParameter)
预加载小程序。
config
: 小程序的配置参数。
-
launch(config: WebAppPreloadParameter): IMiniApp
启动小程序。
config
: 小程序的配置参数。onDismissListener
: 小程序关闭时的回调函数。
-
launchWithUrl(context: Context, owner: LifecycleOwner, uri: Uri, isLaunchUrl: Boolean = false, isGame: Boolean = false, useWeChatStyle: Boolean = true, onDismissListener: (() -> Unit)? = null): IMiniApp?
根据小程序的 URL 启动小程序。
context
: Android 上下文对象。owner
: 生命周期所有者。uri
: 小程序的 URL。isLaunchUrl
: 是否为启动 URL。isGame
: 是否为游戏类型的小程序。useWeChatStyle
: 是否使用微信样式。onDismissListener
: 小程序关闭时的回调函数。
-
attachToContainer(config: WebAppParameters, parentView: ViewGroup, layoutParams: LayoutParams, onDismissListener: (() -> Unit)? = null): IMiniApp
将小程序内嵌到容器中并启动。
config
: 小程序的配置参数。parentView
: 包含小程序视图的 ViewGroup 容器。layoutParams
: 小程序视图的布局参数。onDismissListener
: 小程序关闭时的回调函数。
返回值类型:
IMiniApp
: 小程序实例。
以上是 MiniAppClient
接口的方法及其说明。通过实现这些方法,可以完成小程序客户端的初始化、启动和其他相关操作。
IResourcesProvider
interface IResourcesProvider {
fun getColor(key: String): Int
fun getString(key: String): String
fun isDark(): Boolean
}
接口说明:
IResourcesProvider
接口定义了一个资源提供者,用于自定义小程序的资源,包括颜色、字符串和主题风格。
方法
-
getColor(key: String): Int
获取指定键值的颜色。
key
: 颜色的键值。
-
getString(key: String): String
获取指定键值的字符串。
key
: 字符串的键值。
-
isDark(): Boolean
检查当前主题是否为暗色主题。
返回值类型
Int
: 颜色值。String
: 字符串值。Boolean
: 是否为暗色主题。
以上是 IResourcesProvider
接口的方法及其说明。通过实现这些方法,可以自定义小程序的资源,包括颜色、字符串和主题风格。
IAppDelegate
interface IAppDelegate {
fun attachWithAction(action: String, payload: String): Boolean
fun switchInlineQuery(query: String, types: List<String>)
suspend fun callCustomMethod(method: String, params: JSONObject?): JSONObject
suspend fun scanQrCodeForResult(subTitle: String?): String
suspend fun checkPeerMessageAccess(): Boolean
suspend fun requestPeerMessageAccess(): Boolean
suspend fun sendMessageToPeer(content: String?): Boolean
suspend fun requestPhoneNumberToPeer(): Boolean
suspend fun getBiometryInfo(): BiometryInfo?
suspend fun requestBiometryAccess(reason: String?): BiometryInfo?
suspend fun requestBiometryAuth(reason: String?): Pair<Boolean, String?>
suspend fun updateBiometryToken(token: String?, reason: String?): Pair<Boolean, String?>
fun openBiometrySettings()
fun shareWebApp(url: String)
}
接口说明:
IAppDelegate
接口定义了小程序客户端的应用委托接口,用于处理小程序的各种操作和事件,包括获取身份令牌、连接App动作、打开会话、执行> 自定义方法、扫描二维码、消息发送和权限请求等。
方法
-
fun attachWithAction(action: String, payload: String): Boolean
连接 App 动作,通过指定动作和参数来触发特定的操作,例如转账选择联系人、打开支付页面等。
action
:动作名称。payload
:动作的参数。- 返回类型:
Boolean
,表示操作是否成功。
-
fun switchInlineQuery(query: String, types: List<String>)
在小程序内部打开新的会话,并传递查询字符串和类型。
query
:查询字符串。types
:会话的类型列表。
-
suspend fun callCustomMethod(method: String, params: JSONObject?): JSONObject
执行自定义方法,通过指定方法名称和参数来调用自定义方法。
method
:方法名称。params
:方法的参数。- 返回类型:
JSONObject
,自定义方法的返回结果。
-
suspend fun scanQrCodeForResult(subTitle: String?): String
扫描二维码并返回扫描结果。
subTitle
:扫描界面的子标题。- 返回类型:
String
,扫描到的二维码内容。
-
suspend fun checkPeerMessageAccess(): Boolean
检测当前会话是否支持并授权了发送消息功能。
- 返回类型:
Boolean
,表示是否支持并授权了发送消息功能。
- 返回类型:
-
suspend fun requestPeerMessageAccess(): Boolean
请求当前会话授权发送消息功能。
- 返回类型:
Boolean
,表示请求是否成功。
- 返回类型:
-
suspend fun sendMessageToPeer(content: String?): Boolean
发送消息给当前会话。
content
:消息内容。- 返回类型:
Boolean
,表示发送消息是否成功。
-
suspend fun requestPhoneNumberToPeer(): Boolean
请求发送手机号到当前会话。
- 返回类型:
Boolean
,表示请求是否成功。
- 返回类型:
-
suspend fun getBiometryInfo(): BiometryInfo?
获取生物认证信息,包括 APP 证书授权、人脸、指纹等。
- 返回类型:
BiometryInfo?
,生物认证信息对象,如果没有可用的生物认证信息,则返回null
。
- 返回类型:
-
suspend fun requestBiometryAccess(reason: String?): BiometryInfo?
请求授权生物认证功能。
reason
:请求原因。- 返回类型:
BiometryInfo?
,授权后的生物认证信息对象,如果授权失败,则返回null
。
-
suspend fun requestBiometryAuth(reason: String?): Pair<Boolean,String?>
请求生物认证。
reason
:请求原因。- 返回类型:
Pair<Boolean,String?>
,表示请求生物认证的结果。如果请求失败,则返回(false,null)
;如果请求成功,则返回(true, token value)
,其中token value
是成功认证的令牌值。
-
suspend fun updateBiometryToken(token: String?, reason: String?): Pair<Boolean,String?>
请求更新生物认证。
token
:新的生物认证令牌。reason
:请求原因。- 返回类型:
Pair<Boolean,String?>
,表示更新生物认证的结果。如果更新失败或没有授权生物认证,则返回(false,null)
;如果更新成功,则返回(true, token value)
,其中token value
是更新后的令牌值。
-
fun openBiometrySettings()
打开生物认证设置界面。
-
fun shareWebApp(url: String)
分享小程序的 URL。
url
:小程序的 URL。
IMiniApp
interface IMiniApp {
fun getWebApp(): IWebApp?
fun requestDismiss(force: Boolean = false): Boolean
}
接口说明:
IMiniApp
接口定义了小程序接口, 应用层可以主动请求关闭小程序,获取WebApp对象,对WebView消息进行扩展定制开发。
方法
-
fun getWebApp(): IWebApp?
获取桥接WebView的对象WebApp。
-
返回类型:
IWebApp
,WebApp接口对象。 -
fun requestDismiss(force: Boolean = false): Boolean?
请求关闭小程序。
-
force:
Boolean
, true为强制进入关闭小程序流程, 默认为false。 -
返回类型:
Boolean
, true为关闭,false当前运行时需要处理其他需要人工确认的操作,如页面返回了上一页面,小程序关闭确认。