Browse Source
[android] Implement foreground notification service (#480)
[android] Implement foreground notification service (#480)
A notification is shown when emulation is active. This, in theory should help preventing Eden for getting destroyed in the background. It is also a nice Q.O.L feature to have. Credits go to t895 for the initial implementation. This was back-ported from older official Citra builds, although with crashes which i fixed myself. Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/480 Reviewed-by: crueter <crueter@eden-emu.dev> Co-authored-by: nyx <contact@innix.space> Co-committed-by: nyx <contact@innix.space>pull/2584/head
committed by
crueter
No known key found for this signature in database
GPG Key ID: 425ACD2D4830EBC6
6 changed files with 130 additions and 0 deletions
-
6src/android/app/src/main/AndroidManifest.xml
-
12src/android/app/src/main/java/org/yuzu/yuzu_emu/YuzuApplication.kt
-
19src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt
-
9src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt
-
79src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ForegroundService.kt
-
5src/android/app/src/main/res/values/strings.xml
@ -0,0 +1,79 @@ |
|||
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project |
|||
// SPDX-License-Identifier: GPL-3.0-or-later |
|||
|
|||
// Copyright 2023 Citra Emulator Project |
|||
// Licensed under GPLv2 or any later version |
|||
// Refer to the license.txt file included. |
|||
|
|||
package org.yuzu.yuzu_emu.utils |
|||
|
|||
import android.app.PendingIntent |
|||
import android.app.Service |
|||
import android.content.Intent |
|||
import android.os.IBinder |
|||
import androidx.core.app.NotificationCompat |
|||
import androidx.core.app.NotificationManagerCompat |
|||
import org.yuzu.yuzu_emu.R |
|||
import org.yuzu.yuzu_emu.activities.EmulationActivity |
|||
|
|||
/** |
|||
* A service that shows a permanent notification in the background to avoid the app getting |
|||
* cleared from memory by the system. |
|||
*/ |
|||
class ForegroundService : Service() { |
|||
companion object { |
|||
const val EMULATION_RUNNING_NOTIFICATION = 0x1000 |
|||
|
|||
const val ACTION_STOP = "stop" |
|||
} |
|||
|
|||
private fun showRunningNotification() { |
|||
// Intent is used to resume emulation if the notification is clicked |
|||
val contentIntent = PendingIntent.getActivity( |
|||
this, |
|||
0, |
|||
Intent(this, EmulationActivity::class.java), |
|||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT |
|||
) |
|||
val builder = |
|||
NotificationCompat.Builder(this, getString(R.string.app_notification_channel_id)) |
|||
.setSmallIcon(R.drawable.ic_stat_notification_logo) |
|||
.setContentTitle(getString(R.string.app_name)) |
|||
.setContentText(getString(R.string.app_notification_running)) |
|||
.setPriority(NotificationCompat.PRIORITY_DEFAULT) |
|||
.setOngoing(true) |
|||
.setVibrate(null) |
|||
.setSound(null) |
|||
.setContentIntent(contentIntent) |
|||
startForeground(EMULATION_RUNNING_NOTIFICATION, builder.build()) |
|||
} |
|||
|
|||
override fun onBind(intent: Intent): IBinder? { |
|||
return null |
|||
} |
|||
|
|||
override fun onCreate() { |
|||
showRunningNotification() |
|||
} |
|||
|
|||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { |
|||
if (intent?.action == ACTION_STOP) { |
|||
try { |
|||
NotificationManagerCompat.from(this).cancel(EMULATION_RUNNING_NOTIFICATION) |
|||
stopForeground(STOP_FOREGROUND_REMOVE) |
|||
} catch (e: Exception) { |
|||
Log.error("Failed to stop foreground service") |
|||
} |
|||
stopSelfResult(startId) |
|||
return START_NOT_STICKY |
|||
} |
|||
|
|||
if (intent != null) { |
|||
showRunningNotification() |
|||
} |
|||
return START_STICKY |
|||
} |
|||
|
|||
override fun onDestroy() = |
|||
NotificationManagerCompat.from(this).cancel(EMULATION_RUNNING_NOTIFICATION) |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue