[Contribution] Persistent android service

135 views
Skip to first unread message

Ivan Acevedo

unread,
Jan 31, 2017, 6:40:25 AM1/31/17
to Python For Android
Hello everyone.
Here I make my first contribution, a persistent android service for kivy.
This service runs the new p4a service (it is a service that runs another service). This service will continue to run even after the application has closed.
In addition, notifications can also be sent, which when touched will open the application.

Instructions:

Add the following line to the file "AndroidManifest.tmpl.xml":

<service android:name=".Servicio"></service>

Add the java file to the "buildozer.spec" file:

android.add_src = Servicio.java

(Changes to be made to the java file):

Line 1: package your.package.name; //The package name of your app

Line 9: import your.package.name.ServiceYourServiceName; //The package name of your app + ServiceYourServiceName

Line 89: ServiceYourServiceName service = new ServiceYourServiceName(); //Here you place the class name of your service.

Here you must change the name of the class by the name of the class of your service. Example: ServiceS1

Note: you can repeat this line with the name of your other services (if you have more services). Be sure to call the function "service.start (context," ");" For each service you place.

In python:

How to start the service:
from jnius import autoclass
activity = autoclass("org.kivy.android.PythonActivity").mActivity
s = autoclass("your.package.name.Servicio")
service = s()
                     
service.start(activity)

Congratulations, you already have your service running.

How to send notifications from python:

from jnius import autoclass
from plyer.platforms.android import activity
             
s = autoclass("your.package.name.Servicio")
servicio = s()
             
servicio.notificar(activity, "title", "text", "info", "ticker")

Congratulations, you will have a notification, that when the touch starts your application.

I hope you find it useful.
I know that many things can be improved, but that is what I have achieved. If anyone wants to contribute, he can do it. :)
Thanks :)
Servicio.java

Dayllon Chavier

unread,
Feb 2, 2017, 8:08:55 PM2/2/17
to Python For Android
Como que eu encerro um programa em kivy, tipo paro a apicação?
Message has been deleted

Ivan Acevedo

unread,
Feb 3, 2017, 6:06:49 AM2/3/17
to Python For Android
To stop the application:
from jnius import autoclass
activity = autoclass("org.kivy.android.PythonActivity").mActivity
= autoclass("your.package.name.Servicio")

= autoclass("android.content.Intent")
service = s()

intent 
= I(activity, service.getClass())
                   
activity.stopService(intent)

Then I'll try to improve on the code.
Reply all
Reply to author
Forward
0 new messages