Unity Android Studio 設置自啟動應用
前言
最近有需求,需把Unity軟件發布到android平臺后開機啟動應用,在網上查了很多資料,現整理如下
Unity部分
新建項目,平臺設置為android

tips: 需要勾選Export Project以便于導入Android Studio ,使用Unity版本為2021.3.32f1
Android Studio部分 android studio環境配置相關就省略了,如有需要網上有很多大神的教程,請自行查閱
有幾點注意事項:
1.需要修改AndroidManifest.xml里的配置
<!--接收啟動完成的廣播權限-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!--懸浮窗-->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<receiver
android:name=".StartReciver"-----這里應為添加的java腳本名------
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.HOME"/>
</intent-filter>>
</receiver>
2.打開應用權限允許應用在后臺彈出界面、允許應用顯示懸浮窗及打開應用自啟動
3.修改添加java腳本
@Override
public void onReceive(Context context, Intent intent)
{
//設備重啟之后,打開應用
if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){
Intent startIntent=context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()); //new Intent(context,UintyPlayerActivity.class);
//非常重要 如果缺少的話,程序啟動會報錯
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//自啟動APP
context.startActivity(startIntent);
}
}
后記
Android Studio打包時遇見報錯 Unable to make field private final java.lang.String java.io.File.path accessible: module java.base does not "opens java.io" to unnamed module
該問題是因為Gradle版本和Java版本不兼容導致的問題,一般可以通過查找對應版本重新安裝打包來解決(Compatibility Matrix (gradle.org) 可以在這個網站查找Gradle兼容版本信息),不過我在網上查找到資料,發現了一個暴力解法:
打開Android項目下的gradle.properties,在org.gradle.jvmargs配置后面加上:
--add-exports=java.base/sun.nio.ch=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED
關于Gradle下載,請參考網上其他大神的文章,這里就不詳細介紹了

浙公網安備 33010602011771號