一、開始之前
在unity打包的apk中钓试,一般來說贫母,調(diào)用Android方式令哟,通過類似
obj = new AndroidJavaObject("android.content.Intent")
obj .Call<方法返回類型>("方法名", 參數(shù));
這樣的方式調(diào)用來實(shí)現(xiàn)一些簡單的功能症歇,但是當(dāng)實(shí)現(xiàn)比較復(fù)雜的功能或者代碼量比較多的時(shí)候就需要考慮使用jar包或者在unity中引用庫茉贡。由于是在unity中引用卖擅,所以jar包和引用庫的制作和集成和原生開發(fā)有少許不同:
1.1 制作jar包并使用
以在eclipse環(huán)境下制作為例:
- 首先在C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes目錄下拿到class.jar放入jar工程中
- 繼承UnityPlayerActivity蒜危,如果重寫onCreate方法去掉其中的setContentView()
- 編寫自己的功能接口(繼承了的好處就是在編寫時(shí)候可以用UnityPlayer.currentActivity當(dāng)作上下文呼伸,否則就用
UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")
獲取上下文) - 用Export導(dǎo)出Jar放入U(xiǎn)nity 的 Assets/plugins/Android/下
- 在C#腳本中通過
AndroidJavaClass obj = new AndroidJavaClass("類的全路徑"); obj.CallStatic<接口返回類型>("接口名", 參數(shù) );
調(diào)用
1.2 制作引用庫
當(dāng)接口功能中涉及一些權(quán)限身冀,資源使用的時(shí)候僅僅有jar包是不夠的,那么怎么把資源也同時(shí)引用進(jìn)來呢
unity下的引用庫目錄結(jié)構(gòu):
Assets
| Plugins
|--MyCustomResources
|-- libs
|-- res
| |--- values
| |--- values-en
|-- project.properties
|-- Androidmanifest.xml
注意 project.properties和Androidmanifest.xml不能少括享,否則不能成功打包搂根,其他資源參考Android工程結(jié)構(gòu)目錄添加
二、喚起安裝界面
在android 7.0之前铃辖,實(shí)現(xiàn)喚起僅需要就能實(shí)現(xiàn)
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(install);
7.0之后由于系統(tǒng)加入了訪問限制剩愧,系統(tǒng)的安裝程序直接訪問不了安裝包,
對(duì)于面向 Android 7.0 的應(yīng)用娇斩,Android 框架執(zhí)行的 StrictMode API 政策禁止在您的應(yīng)用外部公開 file:// URI仁卷。
如果一項(xiàng)包含文件 URI 的 intent 離開您的應(yīng)用,則應(yīng)用出現(xiàn)故障犬第,并出現(xiàn) FileUriExposedException 異常锦积。
要在應(yīng)用間共享文件,您應(yīng)發(fā)送一項(xiàng) content:// URI歉嗓,并授予 URI 臨時(shí)訪問權(quán)限丰介。
進(jìn)行此授權(quán)的最簡單方式是使用 FileProvider 類。如需了解有關(guān)權(quán)限和共享文件的詳細(xì)信息鉴分,請(qǐng)參閱共享文件哮幢。
--摘自Android developer
由于這個(gè)原因上面的操作在7.0以上設(shè)備上就毫無反映,雖然系統(tǒng)的安全更加可靠了冠场,對(duì)于開發(fā)者來說變得麻煩了家浇。本砰。碴裙。 那就只好配置本應(yīng)用的provider來解決了
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="packagename.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
filepaths文件、
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path path="" name="apk"/>
</paths>
</resources>
應(yīng)用下載的目標(biāo)位置在存儲(chǔ)根目錄点额,在其他目錄要更改paths節(jié)點(diǎn)下的內(nèi)容
更改后的喚起安裝頁面的實(shí)現(xiàn)
if(Build.VERSION.SDK_INT>=24) {
Uri apkUri = FileProvider.getUriForFile(UnityPlayer.currentActivity, UnityPlayer.currentActivity.getPackageName() + ".fileProvider", file);
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.setDataAndType(apkUri, "application/vnd.android.package-archive");
UnityPlayer.currentActivity.startActivity(install);
} else{
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
UnityPlayer.currentActivity.startActivity(install);
}
三舔株、最后
Android的安裝功能實(shí)現(xiàn)起來并不復(fù)雜,主要是怎么在unity下調(diào)用Android的方法以及類庫的引用方式才是需要學(xué)習(xí)和記錄的地方还棱,最后希望能對(duì)閱讀者有所幫助载慈!
plugins下載:https://pan.baidu.com/s/1Hde5zFwI44FsNUYcCVYY4Q 密碼:654a