Service add-ons: Difference between revisions

From Official Kodi Wiki
Jump to navigation Jump to search
>NedBot
m (Robot: Changing Category:How To to Category:How-to)
(Fix dead-end page and edit for concision)
 
(33 intermediate revisions by 16 users not shown)
Line 1: Line 1:
{{oudated}}
Service [[add-ons]] will be automatically started when a [[Profiles|user profile]] logs in or on Kodi startup, and stopped when the user profile logs out. They must offer the '''<code>xbmc.service</code>''' extension point. Here's a typical example of the contents of the [[addon.xml]] file such add-ons need to provide:
XBMC has now the possibility to automatically start addons on startup, making the autoexec.py file obselete. These special addons are called '''service''' addons.


Service addons must offer the '''xbmc.service''' extension point. Here's a typical example of the addon.xml the addon needs to provide:
<syntaxhighlight lang="xml">
 
<source lang="xml">
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.tv.betaseries"
<addon id="service.example"
       name="BetaSeries.com"
       name="Example service"
       version="0.1"
       version="1.0.0"
       provider-name="blinkseb (XBMC)">
       provider-name="">
   <requires>
   <requires>
     <import addon="xbmc.python" version="1.0"/>
     <import addon="xbmc.python" version="2.1.0"/>
   </requires>
   </requires>
   <extension point="xbmc.service"
   <extension point="xbmc.service" library="service.py" />
            library="default.py" start="login|startup">
  </extension>
   <extension point="xbmc.addon.metadata">
   <extension point="xbmc.addon.metadata">
     <platform>all</platform>
     <platform>all</platform>
     <summary lang="en">BetaSeries.com integration.</summary>
     <summary lang="en"></summary>
   </extension>
   </extension>
</addon>
</addon>
</source>
</syntaxhighlight>
 
If your add-on is meant to be run while Kodi is running, you need to periodically check if Kodi is exiting. The add-on is responsible for terminating when Kodi wants to exit. This can be checked by creating an <code>xbmc.Monitor</code> instance and calling the <code>abortRequested()</code> method. To wait for this event instead, call the <code>waitForAbort()</code> method.
 
Below is an example service that prints "hello addon!" every 10 seconds until Kodi exits:


The addon will be automatically started when XBMC starts. You can specify when you want your addon to start, using the '''start''' tag. If it's isn't here, your addon will automatically start after user login. If you want it to start on XBMC startup, you need to set the '''start''' tag to '''startup'''.
<syntaxhighlight lang="python">
import time
import xbmc


'''Note 1: If your addon is meant to be run while XBMC is running, you need to periodically check if XBMC is exiting. To do that, you need to import the '''xbmc''' module, and check if '''xbmc.abortRequested''' is true. Typically, you should do something like that:
if __name__ == '__main__':
    monitor = xbmc.Monitor()
   
    while not monitor.abortRequested():
        # Sleep/wait for abort for 10 seconds
        if monitor.waitForAbort(10):
            # Abort was requested while waiting. We should exit
            break
        xbmc.log("hello addon! %s" % time.time(), level=xbmc.LOGDEBUG)
</syntaxhighlight>


<source lang="python">
== Gotham and earlier ==
import xbmc;
<code>abortRequested()</code> and <code>waitForAbort()</code> are new methods introduced with Kodi [[Archive:Helix|Helix]]. In [[Archive:Gotham API changes|Gotham]] and earlier, use <code>xbmc.sleep</code> and check the <code>xbmc.abortRequested</code> attribute periodically.


while (not xbmc.abortRequested):
Example:
  some code
</source>


'''Note 2: services addons are currently limited to python only'''
<syntaxhighlight lang="python">
import xbmc
if __name__ == '__main__':
    while not xbmc.abortRequested:
        # some code
        xbmc.sleep(500)
</syntaxhighlight>


'''Note 3: The addon won't be launched if it's disabled by the user.'''
== Leia and earlier ==
Prior to Kodi v18 [[Leia]], there was an additional option, <code>start="startup"</code>, that would start the service add-on before the first profile login, but the usefulness of that functionality was found to be negligible, and it was removed.


You'll find more informations about addons [[Add-ons for XBMC (Developement)|here]]


[[Category:How-to]]
[[Category:Add-on development]]

Latest revision as of 01:41, 23 September 2021

Service add-ons will be automatically started when a user profile logs in or on Kodi startup, and stopped when the user profile logs out. They must offer the xbmc.service extension point. Here's a typical example of the contents of the addon.xml file such add-ons need to provide:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="service.example"
       name="Example service"
       version="1.0.0"
       provider-name="">
  <requires>
    <import addon="xbmc.python" version="2.1.0"/>
  </requires>
  <extension point="xbmc.service" library="service.py" />
  <extension point="xbmc.addon.metadata">
    <platform>all</platform>
    <summary lang="en"></summary>
  </extension>
</addon>

If your add-on is meant to be run while Kodi is running, you need to periodically check if Kodi is exiting. The add-on is responsible for terminating when Kodi wants to exit. This can be checked by creating an xbmc.Monitor instance and calling the abortRequested() method. To wait for this event instead, call the waitForAbort() method.

Below is an example service that prints "hello addon!" every 10 seconds until Kodi exits:

import time
import xbmc

if __name__ == '__main__':
    monitor = xbmc.Monitor()
    
    while not monitor.abortRequested():
        # Sleep/wait for abort for 10 seconds
        if monitor.waitForAbort(10):
            # Abort was requested while waiting. We should exit
            break
        xbmc.log("hello addon! %s" % time.time(), level=xbmc.LOGDEBUG)

Gotham and earlier

abortRequested() and waitForAbort() are new methods introduced with Kodi Helix. In Gotham and earlier, use xbmc.sleep and check the xbmc.abortRequested attribute periodically.

Example:

import xbmc
 
if __name__ == '__main__':
    while not xbmc.abortRequested:
        # some code
        xbmc.sleep(500)

Leia and earlier

Prior to Kodi v18 Leia, there was an additional option, start="startup", that would start the service add-on before the first profile login, but the usefulness of that functionality was found to be negligible, and it was removed.