request_background_fetch

background.request_background_fetch()

This function is used to start fetching information in background. It tells the system to execute the script from which the function is called multiple times a day. The OS decides when it’s appropiate to perform the fetch, so it’s pretty unreliable. The script cannot take more than 30 seconds to execute.

Note

The script to be executed will be __main__.__file__ and is replaced after each request_background_fetch call.

For example, you can write a script for sending notifications or updating a widget.

import background as bg
import notifications as nc
import widgets as wd
import time

bg.request_background_fetch()

current_time = time.strftime("It is %H:%M")

# Send notification

notif = nc.Notification()
notif.message = current_time
nc.send_notification(notif)

# Update widget

time_label = wd.Text(current_time)

widget = wd.Widget()
for layout in (
    widget.small_layout,
    widget.medium_layout,
    widget.large_layout):

        layout.add_row([time_label])

wd.save_widget(widget, "Fetching Test")