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();
        }
    }
}

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

相关推荐
蜡台7 分钟前
Kotlin 零基础完整版实战教程|从语法入门到Android工程实战
android·开发语言·kotlin
律宏阔43 分钟前
Android 车载 USB 开发笔记:USB Host、USB 串口、USB-CAN、HID 与系统 API
android
律宏阔1 小时前
Android 车载串口开发笔记:UART、RS232、RS485、串口配置与数据通信
android
YF02112 小时前
遥控器APP端自动重连方案
android
执明wa2 小时前
Android Studio 打包 APK
android·ide·android studio
waiting9711183 小时前
Ubuntu 26.04 + Android14安装与编译教程
android·linux·ubuntu
hunterandroid3 小时前
Android 测试全景:从单元测试到 UI 自动化的完整实践
android·前端
leoZ2313 小时前
第 8 篇:与 AI 协作的工作流 + 完整案例
前端·人工智能·神经网络·自然语言处理·性能优化·c#·php
蜡台3 小时前
Kotlin 五大作用域函数详解|let/run/apply/also/with 选型指南+实战避坑
android·java·kotlin
2401_833269305 小时前
Android registerForActivityResult详解
android