The Android platform setting program is automatically started at boot

1. Unity export Android project

        First export the Unity project as an Android project, pay attention to select the build platform as Android.

        Just check Export Project. 

2. Android project configuration

        Open the Android project with Android Studio.

1. Gradle upgrade

        Since the Gradle version of the Android project built by Unity is relatively low, it needs to be upgraded, otherwise an error will be reported when compiling the Android project. Just follow the prompts in the pop-up windows of Android Studio to upgrade.

         Click Run selected steps, you only need to upgrade to 4.2.2 once.

        Note: Since it has been upgraded before, there is no need to upgrade again. 

2. Compile SDK version and target SDK version settings

        Set the SDK version in build.gradle of launcher and unityLibrary to 30 or above.

 

3. Create a new boot broadcast receiver 

        Create a new java class and inherit BroadcastReceiver as a boot broadcast receiver.

        Note that it needs to be created under the com.unity3d.player package name in the java directory of unityLibrary.

        Just right-click New-->Java Class on unityLibrary.

        The sample code is as follows:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class StartReceiver extends BroadcastReceiver {
    public StartReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        //设备重启之后,打开应用
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent startIntent = new Intent(context, UnityPlayerActivity.class);
            //非常重要,如果缺少的话,程序将在启动时报错
            startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //自启动APP(Activity)
            context.startActivity(startIntent);
        }
    }
}

 4. Add power-on broadcast receiving permission

        Add it in the AndroidManifest.xml file of unityLibrary.

        Note that it is added in the manifest node.

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

5. Register broadcast receiver

        Register in the AndroidManifest.xml file of unityLibrary.

        Note that it is registered in the application node and is juxtaposed with the activity node.

<receiver
    android:name=".StartReceiver"
    android:enabled="true"
    android:exported="true">
  <intent-filter android:priority="1000">
    <action android:name="android.intent.action.BOOT_COMPLETED"></action>

    <category android:name="android.intent.category.HOME" />
  </intent-filter>
</receiver

6. Build APK

        Build-->Build Bundle(s)/APK(s)-->Build APK(s)。

3. Matters needing attention

1. Android version problem

        On devices with Android version 4.0 and above, you need to manually start the program once, and let the device trust the program before it can take effect and enable self-starting.

2. PICO4 equipment problem

        For PICO4 devices with system permissions disabled, this method is invalid.

Guess you like

Origin blog.csdn.net/qq_40364278/article/details/131578222