After days of testing, I solved the problem

My previous understanding of background operation is indeed problematic, or my solution is just one of the ways to achieve the goal.

in main.py, define the service and start it:

from jnius import autoclass
SERVICE_NAME = u'{packagename}.Service{servicename}'.format(
    packagename=u'org.kivy.test',
    servicename=u'Myservice'
)
service = autoclass(SERVICE_NAME)
mActivity = autoclass(u'org.kivy.android.PythonActivity').mActivity
argument = ''
service.start(mActivity, argument)

in buildozer.spec:

# (str) Package name
package.name = test

# (str) Package domain (needed for android/ios packaging)
package.domain = org.kivy

# (list) List of service to declare
services = Myservice:service.py

and then, edit the service.py as needed. The main.py and service.py can communicate using oscpy.

By doing the above, the notification can be popped up after open APP, even the APP is switched to the background.

Answer from KuMasann on Stack Overflow
🌐
GitHub
github.com › kivy › kivy › wiki › Background-Service-using-P4A-android.service
Background Service using P4A android.service · kivy/kivy Wiki · GitHub
Note: Last Updated 04SEP2017 in reference to changes for Kivy 1.10 and SDL2 boot strap. Create your service as normal in the folder service/ as main.py. To make it a foreground service with a touchable notification; use the code below. import jnius Context = jnius.autoclass('android.content.Context') Intent = jnius.autoclass('android.content.Intent') PendingIntent = jnius.autoclass('android.app.PendingIntent') AndroidString = jnius.autoclass('java.lang.String') NotificationBuilder = jnius.autoclass('android.app.Notification$Builder') Notification = jnius.autoclass('android.app.Notification') service_name = 'S1' package_name = 'com.something' service = jnius.autoclass('org.kivy.android.PythonService').mService # Previous version of Kivy had a reference to the service like below.
Author   kivy
🌐
GitHub
github.com › kivy › kivy › wiki › Starting-Kivy-App-and-Service-on-bootup-on-Android
Starting Kivy App and Service on bootup on Android
Because on boot up there is no Python interpreter, the BroadcastReceiver cannot be coded in Python and adb logcat will report Android unable to load the Java class version of it. So it must be written in Java. Here is MyBroadcastReceiver.java that starts Kivy App (not Service!):
Author   kivy
Discussions

python - How to keep kivy service running in background in Android (service still run when switch to other App or lock the screen)? - Stack Overflow
I'm trying to build an android APP with kivy,my requirement is quit simple: When open the android APP,it will start counting from 0, and a prompt will pop up in the status bar every 5 seconds. The ... More on stackoverflow.com
🌐 stackoverflow.com
August 2, 2020
How to start service in python kivy on android in background mode? - Stack Overflow
I need a service for my app on android to receive message from server. Code of my app: from kivy.app import App from kivy.uix.floatlayout import FloatLayout import sys import socket import time, More on stackoverflow.com
🌐 stackoverflow.com
April 14, 2020
kivy apps background service for android

see: https://github.com/Android-for-Python/Android-for-Python-Users

More on reddit.com
🌐 r/kivy
3
2
July 31, 2021
python - Kivy android service in background and customization - Stack Overflow
In Kivy you can start an android service using AndroidService mechanism from python-for-android. But how can I customize this Service? In exact: change the icon of the permanent notification not s... More on stackoverflow.com
🌐 stackoverflow.com
June 4, 2014
🌐
Kivy
blog.kivy.org › 2014 › 01 › building-a-background-application-on-android-with-kivy
Building a background application on android with Kivy. – Kivy Blog
Kivy runs on android using the python-for-android project, which support android services. This works in a simple way, you basically bundle two main.py in your application, one for the UI, one for the service. The UI can start the services on start. From that point though, things may be a little ...
🌐
Kivy
kivy.org › doc › stable › guide › android.html
Kivy on Android — Kivy 2.3.1 documentation
This includes code for billing/IAP and creating/accessing Android services, which is not yet available in the other tools above. These sections previously described the existence of Kivy’s Android build tools, with their limitations and some devices that were known to work.
🌐
GitHub
github.com › kivy › python-for-android › blob › master › doc › source › services.rst
python-for-android/doc/source/services.rst at master · kivy/python-for-android
To run the services (i.e. starting them from within your main app code), you must use PyJNIus to interact with the java class python-for-android creates for each one, as follows: from jnius import autoclass service = autoclass('your.package.domain.package.name.ServiceMyservice') mActivity = autoclass('org.kivy.android.PythonActivity').mActivity argument = '' service.start(mActivity, argument)
Author   kivy
🌐
GitHub
github.com › sorgmi › kivy_android_service_demo
GitHub - sorgmi/kivy_android_service_demo: Kivy (KivyMD) example using android services and push notifications
Demo App which shows how to use push notifications and android services in your KivyMD App.
Starred by 14 users
Forked by 2 users
Languages   Python 100.0% | Python 100.0%
🌐
GitHub
github.com › tshirtman › kivy_service_osc
GitHub - tshirtman/kivy_service_osc: simple UI/Service communication using osc on python-for-android
This code aims at demonstrating an use of services in python-for-android, and communication between services and a kivy front end.
Starred by 63 users
Forked by 20 users
Languages   Python 100.0% | Python 100.0%
Find elsewhere
🌐
Readthedocs
kivy.readthedocs.io › en › latest › guide › android.html
Kivy on Android — Kivy 2.2.0 documentation
This includes code for billing/IAP and creating/accessing Android services, which is not yet available in the other tools above. These sections previously described the existence of Kivy’s Android build tools, with their limitations and some devices that were known to work.
🌐
Stack Overflow
stackoverflow.com › questions › 61204817 › how-to-start-service-in-python-kivy-on-android-in-background-mode
How to start service in python kivy on android in background mode? - Stack Overflow
April 14, 2020 - I need a service for my app on android to receive message from server. Code of my app: from kivy.app import App from kivy.uix.floatlayout import FloatLayout import sys import socket import time, subprocess from kivy.uix.label import Label from jnius import cast from jnius import autoclass from kivy.context import get_current_context class MyApp(App): def build(self): fl = FloatLayout() try: service = autoclass('org.test.myapp.ServiceMyservice') mActivity = autoclass('org.kivy.android.PythonActivity').mActivity service.start(mActivity, "") except Exception as error: fl.add_widget(Label(text=str(error), font_size=(30))) def on_stop(self): return True if __name__ == '__main__': MyApp().run()
🌐
Reddit
reddit.com › r/kivy › kivy apps background service for android
r/kivy on Reddit: kivy apps background service for android
July 31, 2021 -

guys if anyone knows can u pls tell how to make a kivy app run in background for android.....

waiting for ur reply.. thanks in advance !!!!

🌐
GitHub
github.com › kivy › kivy › wiki › Android-Background-Services
Android Background Services · kivy/kivy Wiki · GitHub
December 14, 2020 - Open source UI framework written in Python, running on Windows, Linux, macOS, Android and iOS - Android Background Services · kivy/kivy Wiki
Author   kivy
🌐
Stack Overflow
stackoverflow.com › questions › 24033526 › kivy-android-service-in-background-and-customization › 24535396
python - Kivy android service in background and customization - Stack Overflow
June 4, 2014 - In Kivy you can start an android service using AndroidService mechanism from python-for-android. But how can I customize this Service? In exact: change the icon of the permanent notification not s...
🌐
GitHub
github.com › sorgmi › kivy_android_service_demo › blob › main › service.py
kivy_android_service_demo/service.py at main · sorgmi/kivy_android_service_demo
Kivy (KivyMD) example using android services and push notifications - sorgmi/kivy_android_service_demo
Author   sorgmi
🌐
GitHub
github.com › kivy › kivy › issues › 7018
Kivy Android Service crash main app · Issue #7018 · kivy/kivy
July 27, 2020 - from kivy.lang import Builder from kivy.utils import platform from kivy.uix.button import Button from kivy.uix.floatlayout import FloatLayout from kivy.uix.button import Button class ServiceApp(App): def build(self): self.layout = FloatLayout() Mbutton = Button( text='start',size_hint=(.2, .1), pos_hint={"center_x": 0.2, "center_y": 0.5},color=[1,1,1,1] ) Mbutton.bind(on_press=self.butcheck) self.layout.add_widget(Mbutton) return self.layout def butcheck(self,instance): from androidф import AndroidService service = AndroidService('my pong service', 'running') service.start('service started') self.service = service if __name__ == '__main__': ServiceApp().run()
Author   Server48
🌐
Inclem
inclem.net › files › p4a_revamp_doc › old_toolchain › android.html
Python API — python-for-android 0.1 documentation - inclem.net
from android.billing import BillingService from kivy.clock import Clock class MyBillingService(object): def __init__(self): super(MyBillingService, self).__init__() # Start the billing service, and attach our callback self.service = BillingService(billing_callback) # Start a clock to check billing service message every second Clock.schedule_interval(self.service.check, 1) def billing_callback(self, action, *largs): '''Callback that will receive all the events from the Billing service ''' if action == BillingService.BILLING_ACTION_ITEMSCHANGED: items = largs[0] if 'org.kivy.gopremium' in items: print "Congratulations, you have a premium acess" else: print "Unfortunately, you don't have premium access" def buy(self, sku): # Method to buy something.
🌐
Google Groups
groups.google.com › g › kivy-users › c › _hciJ7MWJ6M
kivy - android app - background service that runs forever
June 27, 2018 - You do do it this way: https://blog.kivy.org/2014/01/building-a-background-application-on-android-with-kivy/ That also shows how to do the comms. MVP is not required. ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... Just wanted to resurrect this old thread... the guide provided is good, but OP was asking how to use GPS with the background service and get continuous position updates even when the screen is off, app has changed, etc.
🌐
Reddit
reddit.com › r/kivy › examples of kivy with service backend?
r/kivy on Reddit: Examples of kivy with service backend?
February 20, 2019 -

Hey all, been working on an app for a while, when getting to the final home stretch I realized the way I've been doing things has a total dealbreaker without me more or less learning an area I can't even quite find out how to scratch the surface of. In short my apps functionality depends quite largely on timers etc... letting the user know when certain times are up etc... 150 or so hours into it I made the realization that standard behavior is going to pause the app whenever the screen locks or the user switches apps. Which effectively nullifies the entire point of what I want to make. It needs to be able to either play sounds or send notifications regardless of whether the user keeps the phone awake etc...

Researching on my own, I know I need a background service, of which I can find decent documentation on how to start a service. But I constantly am finding only tiny fragments of what a service actually looks like, and I have 0 clues of how to actually send data back and forth between said service and the gui app itself etc...

For the most part I learn well by finding apps with at least some portion of what I'm doing, seeing everything in context and learning from that. When it comes to any apps that use services to do things, or send notifications back and forth, I'm pretty much hitting a desert there. Anyone know of any apps of which the code is availible that has a background service that gets data from a kivy app, and sends some forms of notification? Or any documentation that has large amounts of explanations of how to code something like that with context?

I can find quite a few links to say https://python-for-android.readthedocs.io/en/latest/services/ but that tells me very little beyond how to start a service, I don't know what a service should look like, how to pass data back and forth between that and the main program etc...

🌐
LogRocket
blog.logrocket.com › home › build an android application with kivy python framework
Build an Android application with Kivy Python framework - LogRocket Blog
June 4, 2024 - rectory to exclude (let empty to ... declare #services = NAME:ENTRYPOINT_TO_PY,NAME2:ENTRYPOINT2_TO_PY # # OSX Specific # # # author = © Copyright Info # change the major version of python used by the app osx.python_version = 3 # Kivy version to use osx.kivy_version = 1.9.1 # # Android specific # ...
🌐
Google Groups
groups.google.com › g › kivy-users › c › CDF9nek15nE
Android service from kivy/pyjnius
I would like to know how kivy/pyjnius finds custom java classes. Intent = autoclass('android.content.Intent') PythonActivity = autoclass('org.renpy.android.PythonActivity') activity = cast('android.app.Activity', PythonActivity.mActivity) context = activity.getApplicationContext() #something like this in Java: #Intent intent = new Intent(context, AndroidService.class); intent = Intent() intent.setClassName(context, 'org.myapp.AndroidService') #this is where I'm stuck #source file built from dist/default/src/org/myapp/AndroidService.java context.startService(intent) I put the service tag inside the AndroidManifest.xml file before running the build script, but the service is still "not found".