hyperroid:: fixes to keep-in-memory notifications for newer Android

This commit is contained in:
Zeno Rogue committed 2026-11-07 09:08:10 +01:00
1 parent af717dda52
commit eebf20b08c
3 files changed
+30 -7

No files matched your search

+8 -1
View File
@@ -6,6 +6,8 @@
<!-- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> -->
<!-- <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="9" /> -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application android:label="@string/app_name"
android:icon="@drawable/icon"
android:allowBackup="true">
@@ -25,7 +27,12 @@
android:name=".HyperSettings">
</activity>
<service android:name=".ForegroundService" android:exported="false">
<service android:name=".ForegroundService" android:exported="false"
android:foregroundServiceType="specialUse"
>
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="Keep the game process running while the user temporarily switches to another application." />
</service>
<provider
@@ -30,7 +30,7 @@ public class ForegroundService extends Service {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return "";
String NOTIFICATION_CHANNEL_ID = "com.roguetemple.hyperrogue";
String channelName = "HyperRogue game in progress";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_LOW);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
@@ -41,7 +41,9 @@ public class ForegroundService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals(STARTFOREGROUND_ACTION)) {
if (intent == null || intent.getAction() == null)
stopSelf();
else if (intent.getAction().equals(STARTFOREGROUND_ACTION)) {
Intent notificationIntent = new Intent(this, HyperRogue.class);
@@ -13,6 +13,7 @@ import javax.microedition.khronos.egl.EGLDisplay;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ForegroundServiceStartNotAllowedException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
@@ -33,6 +34,7 @@ import android.net.Uri;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.EGLConfigChooser;
import android.os.Bundle;
import android.os.Build;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.text.ClipboardManager;
@@ -371,11 +373,23 @@ public class HyperRogue extends Activity implements SensorEventListener {
if (keep) {
Intent startIntent = new Intent(HyperRogue.this, ForegroundService.class);
startIntent.setAction(ForegroundService.STARTFOREGROUND_ACTION);
startService(startIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
try {
startForegroundService(startIntent);
} catch (ForegroundServiceStartNotAllowedException e) {
Log.e("HyperRogue", "Cannot start foreground service", e);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(startIntent);
} else {
startService(startIntent);
}
} else {
Intent stopIntent = new Intent(HyperRogue.this, ForegroundService.class);
stopIntent.setAction(ForegroundService.STOPFOREGROUND_ACTION);
startService(stopIntent);
// Intent stopIntent = new Intent(HyperRogue.this, ForegroundService.class);
// stopIntent.setAction(ForegroundService.STOPFOREGROUND_ACTION);
// startService(stopIntent);
stopService(new Intent(HyperRogue.this, ForegroundService.class));
}
if (backgroundmusic != null) backgroundmusic.stop();