Android 分享页面下载app后 自动填充邀请码

android 分享出去h5页面,通过页面下载对应包,安装启动登录注册时候,需要自动填写邀请码,一般情况下,我们需要将分享人信息写入app包中,这样下载时候根据这个信息可以获取到对应安装包。

android我们可以把信息写入assets文件夹下,打包时候assets不会被编译。所以在app安装后,只需要读取对应assets文件,获取对应邀请码。

复制代码
 /**
     * 获取本地写入邀请码文件内容
     *
     * @param context
     * @return
     */
 public static String popularizeInvitationCode(Context context) {
        String invitationCode = null;
        AssetManager assetManager = null;
        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        try {
            if (context != null) {
                assetManager = context.getAssets();
                if (assetManager != null) {
                    inputStream = assetManager.open("popularize-invitation-code.properties");
                    if (inputStream != null) {
                        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                        StringBuffer stringBuffer = new StringBuffer();
                        while (null != (invitationCode = bufferedReader.readLine())) {
                            stringBuffer.append(invitationCode);
                        }
                        return stringBuffer.toString();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
            //由于assetManager 只直接获取系统资源对应,不是自己new出来。所以不能close,
            //关闭会导致系统资源关闭,无法加载,引发资源找不到异常。
            //    if (assetManager != null){
            //        assetManager.close();
            //    }
                if (inputStream != null) {
                    inputStream.close();
                }
                if (bufferedReader != null) {
                    bufferedReader.close();
                }

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

        return invitationCode;
    }

重点:assetManager在open之后千万不要关闭 //assetManager.close();

当用户点击邀请时候,我们告诉后台生成对应app包。被邀请用户点击下载时候,下载指定app包实现,邀请码自动填充

相关推荐
alexhilton3 小时前
Room Fts4:全文搜索替代 SQL LIKE
android·kotlin·android jetpack
hai_android8 小时前
把 Android App 打包成系统应用(System App)
android·kotlin·android jetpack
JMchen9 小时前
【Jetpack 内核实战 01】开篇:Jetpack 组件全家桶核心逻辑与专栏完整规划
android·kotlin·android jetpack
骑着蜗牛撵大象32711 小时前
Linux 进程间通信之管道:从 pipe() 的内核语义到 FIFO 落地排错
android·java·linux·系统编程·fifo·管道·进程间通信
hunterandroid11 小时前
Retrofit 网络请求的异常处理与重试策略实战
android
sejinan11 小时前
极致轻量与秒级起播:NativeWasmTv 开源硬核技术解析与升级版体验
android·开源·webview·tv
小羊没烦恼!12 小时前
Ajax与JSON的一些总结
android·java·前端·数据库·mysql·算法·okhttp
mmsx12 小时前
Android 静默失败实录:五个没有日志的线上事故
android·kotlin
hai_android13 小时前
从零实现一个支持跨进程(IPC)的自定义 Toast
android·kotlin·android jetpack
龙腾-虎跃13 小时前
基于 SIP 协议的自研电话终端:FreeSWITCH 语音与视频通信实战
android·音视频·freeswitch·sip协议终端·wertc