Android 微信小程序支付后跳转不回App的问题

最近做了一个跳转微信小程序支付的功能,但是发现支付完成后跳转不回原App。

一、产生原因

微信的SDK文档中要求在app主模块的包名目录下创建wxapi的文件夹,然后创建一个WXPayEntryActivity,但是我们的工程是一个组件化项目,只有商城shop模块才需要用到支付,并且app模块本来就是一个壳,塞这些微信的东西进去就很恶心。

二、解决办法

使用activity代理的方式重定向微信的Acticity。

app主模块的清单文件中使用代理:

kotlin 复制代码
<application    //放在application根节点中
    android:name="包名.base.BaseApplication"
    android:requestLegacyExternalStorage="true">
    ...
    <!-- WXEntryActivity的代理,用于微信支付跨组件调用 -->
    <activity-alias
        android:name="${applicationId}.wxapi.WXEntryActivity"
        android:exported="true"
        android:targetActivity="包名.shop.WXEntryActivity"/>   //这里的WXEntryActivity可以随便改名字,且不需要放在wxapi文件夹下
</application>

然后在shop模块定义一个代理的目标Activity即可。

kotlin 复制代码
<activity
    android:name=".WXEntryActivity"
    android:exported="true"
    android:launchMode="singleTask"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

在代理Activity(名字随便取,但建议与WXEntryActivity同名好找好维护)中实现微信的回调逻辑即可:

kotlin 复制代码
class WXEntryActivity : BaseActivity<BaseViewModel, ActivityWxpayEntryBinding>(),
    IWXAPIEventHandler {

    private var api: IWXAPI? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        api = WXAPIFactory.createWXAPI(this, UnifyPayPlugin.getInstance(this).appId)
        api?.handleIntent(intent, this)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        api?.handleIntent(intent, this)
    }

    override fun onReq(baseReq: BaseReq?) {
    }

    override fun onResp(baseResp: BaseResp?) {
        if (baseResp?.type == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {   //小程序回调
            //处理微信的结果回调
           ...
        }
        finish()
    }
}
相关推荐
alexhilton2 小时前
Android XR 开发入门完全指南
android·kotlin·android jetpack
2501_915909067 小时前
全面解析iOS应用上架到App Store的完整流程与关键注意事项
android·macos·ios·小程序·uni-app·cocoa·iphone
淡淡的香烟9 小时前
AndroidAI使用心得
android
chjif10 小时前
Android 设备管控开发实战:无障碍如何准确识别当前前台 App?
android
chjif10 小时前
Android 设备管控开发实战:企业受控终端的 DNS 域名策略实现
android·华为·harmonyos
c2385611 小时前
MySQL 基础用法(下):查询进阶与核心特性
android·c语言·c++·mysql
math_hongfan12 小时前
鸿蒙Agent高级技能自定义开发:自定义意图服务/能力注册/Agent技能编排/服务插件开发实战
android·学习·华为·harmonyos·鸿蒙
2501_9151063213 小时前
iOS 证书类型及作用说明 账号证书与签名配置的完整解读
android·ios·小程序·https·uni-app·iphone·webview
九皇叔叔13 小时前
RHEL9 安装 MySQL 8.4.11.LTS 版本
android·mysql·adb
海兰13 小时前
【数据库】tdsql(MySQL )的事务隔离级别
android·数据库·mysql