python service foreground can't open the app but instead gets stuck in a black screen

208 Views Asked by At

Hello guys i have this minimal code where the issue is generated:

from kivy.lang import Builder
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget 
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition

Builder.load_file('the.kv')

def start_service():
    from kivy import platform
    if platform == "android":
        from android import mActivity
        from jnius import autoclass
        service = autoclass("org.pck.my_app.ServiceMyservice_5")   
        mActivity = autoclass("org.kivy.android.PythonActivity").mActivity
        service.start(mActivity, "")
        print('starting service')

start_service()

class fscreen(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

class theapp(App):
    def build(self):

        self.screenm = ScreenManager() 

        self.fscreen = fscreen()
        screen = Screen(name = "first screen")
        screen.add_widget(self.fscreen)
        self.screenm.add_widget(screen)

        return self.screenm

theapp().run()  

And i have this service.py:

import time
from jnius import autoclass
from plyer import notification

PythonService = autoclass('org.kivy.android.PythonService')
PythonService.mService.setAutoRestartService(True)

cnt = 0
while True:
    time.sleep(1)
    cnt += 1
    print('service has been running for ' + str(cnt))

And a small widget in the.kv file:

<fscreen>
    Label:
        text: 'Test for Service'
        font_size: root.height*0.05
        pos: root.width*0.3, root.height*0.5
        size: root.width*0.4, root.height*0.1

this a screen after the buildozer deploy:

enter image description here

and this a screen for the foreground from top slide:

enter image description here

and last a black screen:

enter image description here

1

There are 1 best solutions below

0
On

reference here: https://www.youtube.com/watch?v=DSMzCsnocn0&t=34s

try remove "start_service()" ,and add below code under "class theapp(App):"

def on_start(self):
    from kivy import platform
    if platform == "android":
        self.start_service()

and according to your naming in main.py ,remember to make sure modify some items in bulldozer.spec like below:

# (str) Package name
package.name = my_app

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

# add pyjnius to requirements
requirements = pyjunius

# add to service
services = Myservice_5:service.py