AndroidService crashes after starting

135 views
Skip to first unread message

Ari Camblor

unread,
Jan 18, 2017, 6:04:56 PM1/18/17
to Kivy users support
Hi. I'm developing an Android App with python-for-android, Kivy and Buildozer. I'm trying to run an AndroidService but when it starts, after the notification icon appears, the service crashes (not the App).

Reading logcat, I found it says this is the exception found while running the service: 01-18 19:41:31.438 F/DEBUG   (1001): Abort message: 'art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: GetStringUTFChars received NULL jstring'

The code on main App calling the service is:
    def build(self):
       
if platform == 'android':
           
from android import AndroidService
            service
= AndroidService('my service', 'running')
            service
.start('argument')
           
self.service = service




And the service code is:
   
from time import sleep

   
if __name__ == '__main__':
       
while True:
            sleep
(.1)


The service is located on service/main.py, where it has to be. I'm using the new p4a toolchain (android_new), I don't know if that matters. I also tried this methods for calling the service, but they also failed with the same exception.

I'd really like to solve this because I need to run services, any help would be appreciated.

Donald Schwartfeger

unread,
Jan 19, 2017, 3:18:32 PM1/19/17
to Kivy users support

I don't know if it'll help and I haven't compiled it lately but I have previously used the the following service to wake the main app after a time.

import os
from time import sleep, time
from kivy.logger import Logger
from jnius import autoclass
from android.broadcast import BroadcastReceiver
# create java objects
Context = autoclass('android.content.Context')
Intent = autoclass('android.content.Intent')
PendingIntent = autoclass('android.app.PendingIntent')
SystemClock = autoclass('android.os.SystemClock')
AlarmManager = autoclass('android.app.AlarmManager')
PowerManager = autoclass('android.os.PowerManager')
service = autoclass('org.renpy.android.PythonService').mService
pm = service.getSystemService(Context.POWER_SERVICE)
# partial wake_lock to keep service running
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 'TAG')
wl.acquire()


# our alarm callback routine
def cb(*args):
    # turn on screen and keep on for a while after release
    wl2 = pm.newWakeLock(
        PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, 'TAG')
    wl.release()
    wl2.acquire()
    Logger.info("wakelock 2 %s" % str(wl2))
    Logger.info('callback time %f' % time())
    # TODO bring our ui to front?
    wl2.release()
    global waiting
    waiting = False

# start broadcast receiver with what to callback and when
br = BroadcastReceiver(cb, ['provider_changed'])
br.start()
Logger.info('started %s' % str(br) )

am = service.getSystemService(Context.ALARM_SERVICE)
intent = Intent(Intent.ACTION_PROVIDER_CHANGED)
pi = PendingIntent.getBroadcast(service, 0, intent, 0)
# get seconds until wake
sec2go = int(os.getenv('PYTHON_SERVICE_ARGUMENT'))
# set alarm manager to broadcast
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + sec2go*1000, pi)
Logger.info('alarm set for %f' % (time() + sec2go))
waiting = True
while waiting:
    Logger.debug('time: %f' % time())
    sleep(2)

hope it helps Donald
Reply all
Reply to author
Forward
0 new messages