From eb1799646d81083c761160ec6435f5a83f78f68f Mon Sep 17 00:00:00 2001 From: Victor Dvornikov Date: Sun, 3 Apr 2022 18:05:39 +0300 Subject: [PATCH] fix foreground service start for Android O+ --- .../common/build/templates/Service.tmpl.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java index de84ac42bf..e61904b7b3 100644 --- a/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java @@ -1,5 +1,6 @@ package {{ args.package }}; +import android.os.Build; import android.content.Intent; import android.content.Context; import {{ args.service_class_name }}; @@ -20,7 +21,16 @@ protected int getServiceId() { static public void start(Context ctx, String pythonServiceArgument) { Intent intent = getDefaultIntent(ctx, pythonServiceArgument); + //foreground: {{foreground}} + {% if foreground %} + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + ctx.startForegroundService(intent); + } else { + ctx.startService(intent); + } + {% else %} ctx.startService(intent); + {% endif %} } static public Intent getDefaultIntent(Context ctx, String pythonServiceArgument) {