Android跳转动态微信小程序

本文介绍了一种在App中动态跳转至不同微信小程序的方法,通过后台接口动态获取小程序ID,实现灵活更新,无需频繁发布App新版本。文章详细展示了代码实现过程,包括数据请求、解析及跳转流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

想必做App应用开发的,接触过微信小程序的,都知道app应用内可以跳转到微信小程序界面。这方面的应用知识,官方也给了很详细的文档说明。微信小程序官网

题目所示的跳转动态微信小程序,意思是跳转的页面布局固定,但是跳转的微信小程序不固定,微信小程序的id是通过后台接口,动态获取的,这样做的目的是不用因为小程序更换了,相应的app也要做新版本。后台数据接口处理好就ok。

 

 

看上面两张图片,图一是固定的跳转到应用内的页面,等审核通过,下个版本就可以换成跳转小程序的(有人问为什么下个版本上,不这个版本上,审核呗)。图二就是可以跳转到微信小程序的apk,上面的试发型、换发型、发型设计、脸型分析的名字是动态获取的,图标也可以动态获取(图片动态获取这个我们的项目还没弄,后面应该会加上),然后就是点击跳转到相应的微信小程序了。下面上代码(废话一堆,终于说到重点了,-_-||)

 private void requestApplet() {
        RestClient.builder()//数据请求
                .url("这里填获取微信小程序id的接口")
                .success(this::handleApplet)
                .build()
                .post();
    }

    private void handleApplet(String response) {
        JLogger.e(response);//返回数据解析
        JSONObject jsonObject = JSON.parseObject(response);
        if (JConstants.OK.equals(jsonObject.getString("code"))) {
            try {

                final JSONArray jsonArray = jsonObject.getJSONArray("data");
                final int size = jsonArray.size();
                final List<AppletEntity> dataList = new ArrayList<>();
                for (int i = 0; i < size; i++) {
                    final JSONObject jsonData = jsonArray.getJSONObject(i);
                    final String name = jsonData.getString("name");//获取小程序名字
                    final String initial_id = jsonData.getString("initial_id");//获取小程序id
                    AppletEntity entity = new AppletEntity(name, initial_id);//AppletEntity 实体类
                    dataList.add(entity);
                }

                List<AppletEntity> convert = IndexHeaderDataUtil.convert(dataList);//IndexHeaderDataUtil 工具类,避免if else语句

                //获取小程序1、小程序2、小程序3
                AppletEntity entity = convert.get(0);
                AppletEntity entity1 = convert.get(1);
                AppletEntity entity2 = convert.get(2);

                ((TextView) headerView.findViewById(R.id.tv_free_exchange)).setText(entity.getName());
                ((TextView) headerView.findViewById(R.id.tv_hair_design)).setText(entity1.getName());
                ((TextView) headerView.findViewById(R.id.tv_test_face)).setText(entity2.getName());
                //点击事件
                headerView.findViewById(R.id.tv_free_exchange).setOnClickListener(view -> onClickFreeExchange(entity));
                headerView.findViewById(R.id.tv_hair_design).setOnClickListener(view -> startAction(entity1));
                headerView.findViewById(R.id.tv_test_face).setOnClickListener(view -> startAction(entity2));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void onClickFreeExchange(AppletEntity entity) {
        if (TextUtils.isEmpty(entity.getInitial_id())) {
            startSignGoldDelegate();
        } else {
            startAction(entity);
        }
    }


    private void startSignGoldDelegate() {
        if (LoginHelper.isLogin()) {
            String uid = LoginHelper.uid();
            WebViewNoTitleDelegate delegate = WebViewNoTitleDelegate.create("签到",
                    "http://wap.faxingw.cn/wapapp.php?g=Newunder&m=sign&a=index&device=android&uid=" + uid);
            getParentDelegate().getSupportDelegate().start(delegate);
        } else if (launcherListener != null) {
            launcherListener.onLauncherFinish(OnLauncherFinishTag.NOT_SIGNED);
        }

    }

    private void startAction(AppletEntity entity) {
        String appId = Jumei.getConfiguration(ConfigKeys.WE_CHAT_APP_ID); // 填应用AppId
        IWXAPI api = WXAPIFactory.createWXAPI(getContext(), appId);

        WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
        req.userName = entity.getInitial_id(); // 填小程序原始id
        req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;// 可选打开 开发版,体验版和正式版
        api.sendReq(req);
    }

 

AppletEntity:
package com.jm.ec.main.index.applet;

public class AppletEntity {

    private String id;
    private String name;
    private String initial_id;
    private String status;
    private String sort;
    private String time;

    public AppletEntity(String name, String initial_id) {
        this.name = name;
        this.initial_id = initial_id;
    }


    public String getId() {
        return id;
    }

    public AppletEntity setId(String id) {
        this.id = id;
        return this;
    }

    public String getName() {
        return name;
    }

    public AppletEntity setName(String name) {
        this.name = name;
        return this;
    }

    public String getInitial_id() {
        return initial_id;
    }

    public AppletEntity setInitial_id(String initial_id) {
        this.initial_id = initial_id;
        return this;
    }

    public String getStatus() {
        return status;
    }

    public AppletEntity setStatus(String status) {
        this.status = status;
        return this;
    }

    public String getSort() {
        return sort;
    }

    public AppletEntity setSort(String sort) {
        this.sort = sort;
        return this;
    }

    public String getTime() {
        return time;
    }

    public AppletEntity setTime(String time) {
        this.time = time;
        return this;
    }
}

   IndexHeaderDataUtil:

public class IndexHeaderDataUtil {

    public static List<AppletEntity> convert(List<AppletEntity> list) {
        int size = list.size();
        if (size >= 3) {
            return list;
        } else {
            list.add(0, new AppletEntity("免费兑换", ""));
            if (size == 0) {
                list.add(1, new AppletEntity("测脸型", "小程序id"));
                list.add(2, new AppletEntity("发型设计", "小程序id"));
            } else if (size == 1) {
                list.add(1, new AppletEntity("测脸型", "小程序id"));
            }
        }
        return list;
    }
}

以上即为跳转动态小程序的主要方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值