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下载,请参考网上其他大神的文章,这里就不详细介绍了

  

 

 

posted @ 2023-11-13 17:24  hrinho  阅读(344)  评论(0编辑  收藏  举报