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

*注意事项:

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

步骤:
准备工作:
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>

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>

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

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

案例如下
在这里插入图片描述
最终是最重要代码,在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();
        }
    }
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值