My kivy simple notifications app crashes on android, works fine on windows.
I am trying to make a simple application using kivy, plyer and oscpy modules to check the use of services on android. Here is what the app does: It has a button "set" that asks the service to pop a notification for the next minute And another button "stop" to stop the service.
Here is the code:
main.py:
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
import datetime
from kivy.clock import Clock
from kivy.utils import platform
from oscpy.client import OSCClient
from oscpy.server import OSCThreadServer
from plyer import notification
SERVICE_NAME = u'{packagename}.Service{servicename}'.format(
packagename=u'org.kivy.oscservice',
servicename=u'Pong'
)
class MyLayout(FloatLayout):
def __init__(self,**kwargs):
super(MyLayout,self).__init__(**kwargs)
self.setB=Button(text="set",size_hint=(.25,.2),pos_hint={"x":.3,"y":.4})
self.add_widget(self.setB)
self.setB.bind(on_press=self.set)
self.lis=[]
self.server=None
self.server=server=OSCThreadServer()
server.listen(
address=b'localhost',
port=30002,
default=True
)
server.bind(b'/message',self.passnow)
server.bind(b'/date',self.passnow)
self.client=OSCClient(b'localhost',3000)
self.start_service()
self.stop=Button(text="Stop",size_hint=(.25,.2),pos_hint={"x":.3,"y":.1})
self.add_widget(self.stop)
self.stop.bind(on_press=self.stop_service)
def start_service(self):
if platform=='android':
service=autoclass(SERVICE_NAME)
self.mActivity=autoclass(u'org.kivy.android.PythonActivity').mActivity
argument=''
service.start(self.mActivity,argument)
self.service=service
elif platform in ('linux','linux2','macos','win'):
from runpy import run_path
from threading import Thread
self.service=Thread(
target=run_path,
args=['service.py'],
kwargs={'run_name':'__main__'},
daemon=True
)
self.service.start()
else:
raise NotImplementedError(
"service start not implemented on this platform"
)
def stop_service(self):
if self.service:
if platform=="android":
self.service.stop(self.mActivity)
elif platform in ('linux','linux2','macos','win'):
self.service.stop()
else:
raise NotImplementedError(
"service start not implemented on this platform"
)
self.service=None
def set(self,button):
time=datetime.datetime.now().strftime("%H:%M:%S")
if int(time.split(':')[1])+1 not in self.lis:
self.lis.append(int(time.split(':')[1])+1)
q=[]
for p in range(len(self.lis)):
if self.lis[p]==60:
self.lis[p]=0
if self.lis[p]<=int(time.split(':')[1]) and self.lis[p]!=0:
q.append(p)
for p in q:
self.lis.remove(self.lis[p])
# print(self.lis)
self.client.send_message(b'/ping',list(self.lis),)
def passnow(self,message):
pass
class PlyerApp(App):
def build(self):
return MyLayout()
if __name__=="__main__":
now=PlyerApp()
now.run()
and
service.py:
import datetime
from oscpy.server import OSCThreadServer
from oscpy.client import OSCClient
from plyer import notification
from time import sleep
notiflis=[]
CLIENT = OSCClient('localhost',3002)
def ping(*args):
lis=[k for k in args]
global notiflis
notiflis=lis
print(lis)
CLIENT.send_message(
b'/message',[],
)
def plyer():
now=int(datetime.datetime.now().strftime("%H:%M:%S").split(":")[1])
#print(now," ",(now in notiflis))
if now in notiflis:
notification.notify(
title=str('REMINDER AT '+str(datetime.datetime.now().strftime("%H:%M:%S"))),
message="HERE IS YOUR REMINDER",
timeout=15
)
notiflis.remove(now)
else:
return
if __name__=="__main__":
SERVER = OSCThreadServer()
SERVER.listen('localhost',port=3000,default=True)
SERVER.bind(b'/ping',ping)
while True:
plyer()
sleep(.9)
This works fine on windows but when i try to run it on android using Kivy Launcher, it crashes after loading with the error:
NameError: global name 'autoclass' is not defined
autoclass() is used in main.py in the line :
def start_service(self):
if platform=='android':
> service=autoclass(SERVICE_NAME)
Can someone please tell me why the error is occurring and how to solve it?