Wear OS - Keep app in foreground

Unity version: 2022.3.30f1
Windows version: 10 64 Bit Pro
Watch: TicWatch Pro 3

Hello,

I’m desperately trying to solve this issue:
After like 30 seconds (when the display is off) my app is not in foreground anymore.
I always need to re-open it again.

From what I understand I have to copy the AndroidManifest into assets\Plugins\Android
and add the line:

I tried multiple varations, but it either:

  • could not get compiled
  • crashed immediately after starting on the watch
  • app went into background after 30 seconds.

I know it is possible to keep the display on, but this should be that last option since it’s not that good for the battery.

Here is one of the things I tried:
C:\Unity Projects\Wear-Project\Assets\Plugins\Android\src\main\java\com\example\weartest\MainActivity.java

package com.example.weartest;

import android.app.Activity;
import android.os.Bundle;
import android.os.PowerManager;
import android.content.Context;

public class MainActivity extends Activity {

    private PowerManager.WakeLock wakeLock;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "weartest::MyWakelockTag");
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (wakeLock != null && !wakeLock.isHeld()) {
            wakeLock.acquire();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (wakeLock != null && wakeLock.isHeld()) {
            wakeLock.release();
        }
    }
}

My AndroidManifest.xml, which is placed under C:\Unity Projects\Wear-Project\Assets\Plugins\Android:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionCode="6"
    android:versionName="1.5"
    package="com.example.weartest"
    platformBuildVersionCode="22"
    platformBuildVersionName="5.1.1-1819727">

    <uses-sdk
        android:minSdkVersion="18"
        android:targetSdkVersion="22" />

    <uses-feature
        android:name="android.hardware.type.watch" />

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

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

    <application
        android:theme="@ref/0x01030128"
        android:allowBackup="true">

        <uses-library
            android:name="com.google.android.wearable"
            android:required="false" />

        <activity
            android:label="@ref/0x7f070021"
            android:name="com.example.weartest.MainActivity">

            <intent-filter>

                <action
                    android:name="android.intent.action.MAIN" />

                <category
                    android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="android.support.wearable.activity.ConfirmationActivity" />

    </application>
</manifest>

Maybe there is an easier way to implement that function?
Am I on the right way anyway?

Thanks for the help!

Best regards

Hello,

I’ve made some progress.
I was able to solve the problem by using Android Studio and a BroadcastReceiver.
However, since I’m on Unity, I need a way to implement the MainActivity + the BroadcastReceiver.

My MainActivity.kt and ScreenReceiver.kt (copied from the Android Project Folder) are located here:
c:\Users\User\Documents\Unity Projects\Test-Project\Assets\Plugins\Android\src\main\java\com\example\testapp\

My AndroidManifest.xml is located here: c:\Users\User\Documents\Unity Projects\Test-Project\Assets\Plugins\Android
and contains of:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testapp"
    xmlns:tools="http://schemas.android.com/tools"
    android:installLocation="preferExternal">

    <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />

    <uses-feature android:glEsVersion="0x00020000" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.REORDER_TASKS" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY"/>
    <uses-feature android:name="android.hardware.touchscreen" android:required="false" />
    <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
    <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />

    <application android:theme="@style/UnityThemeSelector"
        android:icon="@mipmap/app_icon"
        android:label="@string/app_name">

        <meta-data android:name="unity.build-id" android:value="596190e6-ba05-4016-a652-1aab4c60e527" />
        <meta-data android:name="unity.splash-mode" android:value="0" />
        <meta-data android:name="unity.splash-enable" android:value="True" />
        <meta-data android:name="notch.config" android:value="portrait|landscape" />

        <activity android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".ScreenReceiver"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="android.intent.action.SCREEN_ON" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

Is something like this even possible?
I mean it is like a mix of Unity and Android.

Thanks for any help!

Best regards