1. 程式人生 > >unity+android:大版本更新安裝下好的apk,相容任意安卓5.0,7.0,8.0版本

unity+android:大版本更新安裝下好的apk,相容任意安卓5.0,7.0,8.0版本

Android Studio
1:在專案的res目錄下建立xml目錄,再建立file_paths.xml檔案。
在這裡插入圖片描述

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <root-path name="root" path="" />
    <external-path name="external_storage_root" path="." />
    <external-path name="external_storage_download" path="Download" />
</paths>

2.更改Androidmainfest檔案

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.luoyikun.installapk">

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

    <application
        android:allowBackup="true">

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="cn.com.woong.resultbackdemo.fileProvider"
        android:exported="false"
        android:grantUriPermissions="true"
        tools:ignore="WrongManifestParent">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"/>
    </provider>

    </application>
</manifest>

在這裡插入圖片描述
紅線部分替換為你應用的包名

3:java原始碼

package com.luoyikun.installapk;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.FileProvider;

import com.unity3d.player.UnityPlayer;

import java.io.File;

public class InstallApk extends Fragment {
    private static final String TAG = "InstallApk";

    String m_gameObjectName;
    String m_funcName;

    private Context unityContext;
    private Activity unityActivity;


    public void UnityFunc(String gameObjectName, String funcName)
    {
        m_gameObjectName = gameObjectName;
        m_funcName = funcName;
    }

    public static InstallApk GetInstanceByAndroid(Activity activity,Context context)
    {
        if (Instance == null)
        {
            Instance = new InstallApk();
            Instance.unityActivity = activity;
            Instance.unityContext = context;

            activity.getFragmentManager().beginTransaction().add(Instance, TAG).commit();
        }
        return Instance;
    }

    private static InstallApk Instance = null;

    public static InstallApk GetInstance()
    {
        if(Instance == null)
        {
            Instance = new InstallApk();
            Instance.unityActivity = UnityPlayer.currentActivity;
            Instance.unityContext = Instance.unityActivity.getBaseContext();
            UnityPlayer.currentActivity.getFragmentManager().beginTransaction().add(Instance, TAG).commit();

        }
        return Instance;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);  // 這一句很重要,儲存對該Fragment的引用,防止在旋轉螢幕等操作時時丟失引用(Fragment隸屬於Activity)
    }

    // 呼叫 Unity
    // gameObjectName 為接受訊息的Unity 中 GameObject 的名字
    // functionName   為接受訊息的GameObject 掛載 C# 指令碼中的函式名
    // _content       為傳送給Unity 的內容
    public void CallUnity( String _content)
    {
        UnityPlayer.UnitySendMessage(m_gameObjectName, m_funcName, _content);
    }

    public void installApp(String path){
        File pFile = new File(path);
        if (null == pFile)
            return;
        if (!pFile.exists())
            return;
        Intent _Intent = new Intent();
        _Intent.setAction(Intent.ACTION_VIEW);
        Uri _uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            _Intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            _uri = FileProvider.getUriForFile(unityContext, unityContext.getPackageName() + ".fileProvider", pFile);
        }else {
            _uri = Uri.fromFile(pFile);
            _Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        _Intent.setDataAndType(_uri, "application/vnd.android.package-archive");
        unityContext.startActivity(_Intent);
    }

}

unity:
1.在unity需要匯入android-support-v4.jar、android-support-v7-appcompat.jar包
在這裡插入圖片描述
2.呼叫

    public static void InstallApk(string path)
    {
        AndroidJavaObject pluginObject = new AndroidJavaClass("com.luoyikun.installapk.InstallApk").CallStatic<AndroidJavaObject>("GetInstance");
        pluginObject.Call("installApp", path);
    }

需要注意的是如果使用unity的Application.persistentDataPath會在路徑的最前面多個 : ,這個在安卓端是不認的,所以

            string path = dataPath + apkName;
            path = path.Replace(":", "");
            AndroidFunc.InstallApk(path);