Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion doc/source/services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ The ``PATH_TO_SERVICE_PY`` is the relative path to the service entry point (like
You can optionally specify the following parameters:
- :code:`:foreground` for launching a service as an Android foreground service
- :code:`:sticky` for launching a service that gets restarted by the Android OS on exit/error
- :code:`:foregroundServiceType=TYPE` to specify the type of foreground service,
where TYPE is one of the valid Android foreground service types
(see `Android documentation <https://developer.android.com/develop/background-work/services/fgs/service-types>`__
for more details). You can specify multiple types separated by a pipe
character "|", e.g. :code:`:foregroundServiceType=location|mediaPlayback`. Mandatory
if :code:`:foreground` is used on Android 14+.

Full command with all the optional parameters included would be:
:code:`--service=myservice:services/myservice.py:foreground:sticky`
:code:`--service=myservice:services/myservice.py:foreground:sticky:foregroundServiceType=location`

You can add multiple
:code:`--service` arguments to include multiple services, or separate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@
<service android:name="{{ args.service_class_name }}"
android:process=":pythonservice" />
{% endif %}
{% for name in service_names %}
{% for name, foreground_type in service_data %}
<service android:name="{{ args.package }}.Service{{ name|capitalize }}"
{% if foreground_type %}
android:foregroundServiceType="{{ foreground_type }}"
{% endif %}
android:process=":service_{{ name }}" />
{% endfor %}
{% for name in native_services %}
Expand Down
18 changes: 14 additions & 4 deletions pythonforandroid/bootstraps/common/build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ def make_package(args):
if exists(service_main) or exists(service_main + 'o'):
service = True

service_names = []
service_data = []
base_service_class = args.service_class_name.split('.')[-1]
for sid, spec in enumerate(args.services):
spec = spec.split(':')
Expand All @@ -476,8 +476,18 @@ def make_package(args):

foreground = 'foreground' in options
sticky = 'sticky' in options
foreground_type_option = next((s for s in options if s.startswith('foregroundServiceType')), None)
foreground_type = None
if foreground_type_option:
parts = foreground_type_option.split('=', 1)
if len(parts) != 2 or not parts[1]:
raise ValueError(
'Missing value for `foregroundServiceType` option. '
'Expected format: foregroundServiceType=location'
)
foreground_type = parts[1]

service_names.append(name)
service_data.append((name, foreground_type))
service_target_path =\
'src/main/java/{}/Service{}.java'.format(
args.package.replace(".", "/"),
Expand Down Expand Up @@ -541,10 +551,10 @@ def make_package(args):
render_args = {
"args": args,
"service": service,
"service_names": service_names,
"service_data": service_data,
"android_api": android_api,
"debug": "debug" in args.build_mode,
"native_services": args.native_services
"native_services": args.native_services,
}
if is_sdl_bootstrap():
render_args["url_scheme"] = url_scheme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@
<service android:name="{{ args.service_class_name }}"
android:process=":pythonservice" />
{% endif %}
{% for name in service_names %}
{% for name, foreground_type in service_data %}
<service android:name="{{ args.package }}.Service{{ name|capitalize }}"
{% if foreground_type %}
android:foregroundServiceType="{{ foreground_type }}"
{% endif %}
android:process=":service_{{ name }}" />
{% endfor %}
{% for name in native_services %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
<uses-sdk android:minSdkVersion="{{ args.min_sdk_version }}" android:targetSdkVersion="{{ android_api }}" />

<application {% if debug %}android:debuggable="true"{% endif %} >
{% for name in service_names %}
{% for name, foreground_type in service_data %}
<service android:name="{{ args.package }}.Service{{ name|capitalize }}"
{% if foreground_type %}
android:foregroundServiceType="{{ foreground_type }}"
{% endif %}
android:process=":service_{{ name }}"
android:exported="true" />
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@
android:process=":pythonservice"
android:exported="true"/>
{% endif %}
{% for name in service_names %}
{% for name, foreground_type in service_data %}
<service android:name="{{ args.package }}.Service{{ name|capitalize }}"
{% if foreground_type %}
android:foregroundServiceType="{{ foreground_type }}"
{% endif %}
android:process=":service_{{ name }}"
android:exported="true" />
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@
<service android:name="org.kivy.android.PythonService"
android:process=":pythonservice" />
{% endif %}
{% for name in service_names %}
{% for name, foreground_type in service_data %}
<service android:name="{{ args.package }}.Service{{ name|capitalize }}"
{% if foreground_type %}
android:foregroundServiceType="{{ foreground_type }}"
{% endif %}
android:process=":service_{{ name }}" />
{% endfor %}

Expand Down
Loading