Android 平台Apk安装
1.在res文件夹中新建xml文件夹
2.在xml文件夹中新建一个file_paths.xml文件
3.打开file_paths.xml文件在文件中写入以下内容
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="." path="."/>
</paths>
4.新建一个CarFileProvider.java类,类继承于FileProvider,在文件中写入以下内容
package com.happyhour.zsjy;
import android.support.v4.content.FileProvider;
public class CarFileProvider extends FileProvider {
}
5.新建一个InstallApk.java类
package com.happyhour.zsjy;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import java.io.File;
/**
* Created by Administrator on 2018/8/10.
*/
public class InstallApk {
static String sPackageName ="";
/**
* 安装apk
*
* @param fileSavePath
*/
public static void OnInstallApk(Activity unityActivity,String fileSavePath) {
String sPackageName =unityActivity.getPackageName();
File file = new File(fileSavePath);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri data;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//判断版本大于等于7.0
// "sven.com.fileprovider.fileprovider"即是在清单文件中配置的authorities
// 通过FileProvider创建一个content类型的Uri
data = FileProvider.getUriForFile(unityActivity,sPackageName+".fileprovider", file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// 给目标应用一个临时授权
} else {
data = Uri.fromFile(file);
}
intent.setDataAndType(data, "application/vnd.android.package-archive");
unityActivity.startActivity(intent);
}
}
6.打开 AndroidManifest.xml,在文件<application></application>中写入以下内容
<provider
android:name="com.happyhour.zsjy.CarFileProvider"
android:authorities="com.happyhour.zsjy.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
注:com.happyhour.zsjy是类名