• unity 发布apk,在应用内下载安装apk(用于更大大版本)


    *注意事项:

    •   1,andriod 7.0 和 android 8.0是安卓系统的分水岭,需要分开来去实现相关内容
        2,注意自己的包名,在设置一些共享文件的时候需要放自己的包名
        3,以下是直接用arr包放入unity中直接使用的,不需要导入unity的class.jar包
      
      • 1
      • 2
      • 3

    步骤:
    准备工作:
    1.在main的文件下面新建一个file_paths.xml文件
    代码:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <paths>
            <!--
            external-path: 该方式提供在外部存储区域根目录下的文件。
            它对应Environment.getExternalStorageDirectory返回的路径
            external-files-path: Context.getExternalFilesDir(null)
    
            external-cache-path: Context.getExternalCacheDir(String)
            -->
            <external-path name="download" path="" />
        </paths>
    
    </resources>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.在AndrioManifest.xml的中添加一下代码

           <provider
    
            android:name="androidx.core.content.FileProvider"
            android:authorities= "你的包名"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
            </provider>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    案例代码如下:
    在这里插入图片描述
    3.在unity的plungs的android下面的andriodManifest.xml里新增一句,开启安装的权限

    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
    
    • 1

    案例如下
    在这里插入图片描述
    最终是最重要代码,在androidstudio里面新增一个安装的类InstallUtil,同时解决android 8.0和 android 7.0问题代码如下

    package com.gomore.pigfarm.util;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Build;
    import android.provider.Settings;
    import android.util.Log;
    
    import androidx.annotation.RequiresApi;
    import androidx.core.content.FileProvider;
    
    import java.io.File;
    
    public class InstallUtil {
        private static final int UNKNOWN_CODE = 2020;
        //外部调用
        public static void installApk(Context context, String path) {
    
    
            //8.0及以上
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              //  unityActivity.showToast("8");
                startInstallO(context, path);
                //7.0及以上
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                //unityActivity.showToast("7");
                startInstallN(context, path);
                //7.0 以下
            } else {
                startInstall(context, path);
            }
        }
        /**
         * android1.x-6.x
         *
         * @param path 文件的路径
         */
        public static void startInstall(Context context, String path) {
            Intent install = new Intent(Intent.ACTION_VIEW);
            install.setDataAndType(Uri.parse("file://" + path), "application/vnd.android.package-archive");
    
            install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
            context.startActivity(install);
        }
        /**
         * android7.x
         *
         * @param path 文件路径
         */
        @RequiresApi(api = Build.VERSION_CODES.N)
        public static void startInstallN(Context context, String path) {
    
            String authority = "填自己的包名";
    
            try {
               // unityActivity.showToast("进入组装安装7");
                //参数1 上下文, 参数2 在AndroidManifest中的android:authorities值, 参数3 共享的文件
                Uri apkUri = FileProvider.getUriForFile(context, authority, new File(path));
                Intent install = new Intent(Intent.ACTION_VIEW);
    
                //由于没有在Activity环境下启动Activity,设置下面的标签
                install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
                //添加这一句表示对目标应用临时授权该Uri所代表的文件
                install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                install.setDataAndType(apkUri, "application/vnd.android.package-archive");
                context.startActivity(install);
            } catch (Exception e) {
                e.printStackTrace();
                Log.d("S","startInstallN: " + e.getMessage());
            }
        }
    
        //private static final String TAG = "InstallUtil";
    
        /**
         * android8.x
         */
        @RequiresApi(api = Build.VERSION_CODES.O)
        private static void startInstallO(final Context context, String path) {
    
    
            boolean isGranted = context.getPackageManager().canRequestPackageInstalls();
            if (isGranted) {
                startInstallN(context, path);//安装应用的逻辑(写自己的就可以)
               // unityActivity.showToast("开始安装应用8");
            }
            else {
              //  unityActivity.showToast("开始安装应用未授权");
                new AlertDialog.Builder(context)
                        .setCancelable(false)
                        .setTitle("安装应用需要打开未知来源权限,请去设置中开启权限")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface d, int w) {
                               // unityActivity.showToast("安装8");
                                Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
                                Activity act = (Activity) context;
                                act.startActivityForResult(intent, UNKNOWN_CODE);
                            }
                        }).show();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109

    最后打包运行可以安装,不懂的可留言哈

  • 相关阅读:
    C语言题收录(七)
    Vue3配置路由
    WEB3 创建React前端Dapp环境并整合solidity项目,融合项目结构便捷前端拿取合约 Abi
    Scanner类中nextInt()和nextLine()一起使用时出现的问题
    以太网——ARP协议工作原理
    最近面试被问到的vue题
    scipy短时傅里叶分析STFT
    没有二十年功力,写不出Thread.sleep(0)这一行“看似无用”的代码!
    线上教育需要ICP备案还是许可?
    Spring Cloud学习笔记(Ribbon):Ribbon的应用样例
  • 原文地址:https://blog.csdn.net/qq_40390815/article/details/132605555