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

*注意事项:

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

步骤:

准备工作:

1.在main的文件下面新建一个file_paths.xml文件
代码:

csharp 复制代码
<?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>

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

csharp 复制代码
       <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>

案例代码如下:

3.在unity的plungs的android下面的andriodManifest.xml里新增一句,开启安装的权限

csharp 复制代码
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

案例如下

最终是最重要代码,在androidstudio里面新增一个安装的类InstallUtil,同时解决android 8.0和 android 7.0问题代码如下

java 复制代码
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();
        }
    }
}

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

相关推荐
GEEKVIP1 小时前
摆脱困境并在iPhone手机上取回删除照片的所有解决方案
android·macos·ios·智能手机·电脑·笔记本电脑·iphone
呆萌小新@渊洁5 小时前
后端接收数组,集合类数据
android·java·开发语言
仙魁XAN7 小时前
Unity 设计模式 之 创建型模式 -【单例模式】【原型模式】 【建造者模式】
unity·单例模式·设计模式·建造者模式·原型模式
Clank的游戏栈7 小时前
Unity DOTS物理引擎的核心分析与详解
unity·游戏引擎·全文检索
ByteSaid7 小时前
Android 内核开发之—— repo 使用教程
android·git
向宇it7 小时前
【推荐100个unity插件之34】在unity中实现和Live2D虚拟人物的交互——Cubism SDK for Unity
游戏·unity·游戏引擎·交互
Tom哈哈8 小时前
Android 系统WIFI AP模式
android
ShawnRacine8 小时前
Android注册广播
android
白茶等风121388 小时前
C#_结构(Struct)详解
开发语言·c#
dangoxiba9 小时前
[Unity Demo]从零开始制作空洞骑士Hollow Knight第七集:制作小骑士完整的冲刺Dash行为
游戏·unity·c#·游戏引擎·playmaker