Android Question Forever Widgets

drgottjr

Expert
Licensed User
Longtime User
EDIT: don't bother with this thread. if i could delete it, i would. i was never able to ask the questions in a way that proved useful


i recently updated my little home screen stock market widget app. i asked if there were any conflicts regarding using a timer to
manually trigger a widget update using the new receiver module. erel kindly answered that question.

i followed up with a question that, i think, was lost when the thread unexpectedly turned to a different matter.
https://www.b4x.com/android/forum/threads/home-widgets-w-timer.145493/#post-922478

i'm trying again. erel said, "no problem but you should expect the process to be killed at some point."

my question then essentially was: are all widgets killed at some point? or only mine? i wanted to know if erel's widget example was
killed too. if his wasn't killed, why not? was mine killed because i turned off automatic updates (and used manual updates instead)?

my widget appears to run happily for hours with updates every minute (the receiver simply stops updating the widget when stock markets
are closed, but it keeps checking to see if it should update the widget). i watch videos or listen to the radio and/or test or deploy apps on the
device all while the widget updates. and, of course, android's weather widget seems to run forever.

i thought the os handled widgets differently than user apps that tried to run for long periods or tried to wake themselves up periodically
for long periods. are all widgets killed?
 
Last edited:

JohnC

Expert
Licensed User
Longtime User
Hey @drgottjr,

My impression from that post about receivers is that receivers are designed to only live a short period of time, so you should perform the needed action as quick as possible so it will complete before the receiver is possibly killed.

So, instead of using a timer in the reciever, try doing something like this:

B4X:
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)

    '#####
    'Code to update the widget's stock quotes here
    '#####

    StartReceiverAt(Me, DateTime.Now + (1 * DateTime.TicksPerMinute), True)  'run this receiver again in 1 minute
 
End Sub
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
@JohnC, thanks for response. the same thing is happening that happened when i asked the same question previously: there is no answer to my question.
my receiver only runs for a few seconds (at most), and it runs for hours. i'm not saying your suggestion is wrong. i'm just trying to find out if all widgets are killed at some point. or only mine. and if only mine, why?

a member in greece (hatzin, i think is his name), asked about downloading something within a widget. erel said his tests indicated it falls within a receiver's timeframe.

i clarified something in my post above: the receiver isn't running every minute; it's the widget update that runs every minute. the receiver keeps itself going within the "short time" required to keep it alive. whether i should modify things to use your suggestion, i'm not sure. erel says there is no problem doing it my way.

are all widgets killed? or just mine? if just mine, why?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The process will be started when needed - the user interacts with the widget, or based on the interval setting in the ConfigureHomeWidget call. It shouldn't be less than 30 minutes. The process will not stay alive for too long.
A playback app must start a foreground service while the app is in the foreground. A widget is not enough.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
thanks for the response. still struggling here, sorry; i feel we're talking about different things.

you said it was ok to use a timer (as opposed to using ConfigureHomeWidget to set an interval).

the widget's update takes less than a couple seconds. it does its job every minute. i use the timer to make sure the receiver doesn't time out.

i don't know what you mean by a "playback" app. like, streaming video/audio? my widget isn't such an app. it updates a single line label with a stock price.

if it's ok to use a timer, and the process only takes a couple seconds to run, why did you say the process would be killed? the only real difference
between your widget example and my widget (based on yours) is mine doesn't use ConfigureHomeWidget to set an interval. will your widget be killed too?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
the widget's update takes less than a couple seconds. it does its job every minute. i use the timer to make sure the receiver doesn't time out.
There is no relation between the timer and whether the process will be kept alive or not.

will your widget be killed too?
More or less yes. It is not the widget that is killed. It is the process that is killed. The process is started again when the user interacts with the widget or based on the interval time.
A timer will not have any effect on this behavior.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
getting closer. thanks
The process is started again when the user interacts with the widget or based on the interval time.
what interval time? i set it to 0 when configuring widget.

A timer will not have any effect on this behavior.
without a timer and with interval set to 0, the widget is updated once. only once. when i enable timer, widget runs as expected. timer is doing something.

when you said timer was ok to use, what did you think i was using it for, if not to act as an interval?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
what interval time? i set it to 0 when configuring widget.
You disabled the timed updates.

when you said timer was ok to use, what did you think i was using it for, if not to act as an interval?
There is nothing wrong with using a timer in the receiver code. You need to understand that it will not have any effect on the process lifecycle.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
veil slowly being lifted. thanks. to test your patience a bit more:

does setting an interval > 0 affect the process lifecycle?
or is that just an android move to save resources and battery?
(by ignoring intervals < 30 minutes)

is there a difference between using a timer and using
startreceiverat in terms of process life? i've tried both, and
except for moving some widget update code around a little,
both versions appear to behave the same. or to put it differently,
is startreceiverat the "correct" way?

when i use a timer, receiver_received is only triggered once. yet the
update never needs it. if i use startreceiverat, receiver_received is
triggered at whatever time i set in startreceiverat.

we're told a receiver only runs for a short time. i don't understand how
my widget runs for hours without needing startreceiverat to update it.
the receiver seems to run once and that's it.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
does setting an interval > 0 affect the process lifecycle?
or is that just an android move to save resources and battery?
(by ignoring intervals < 30 minutes)
It must be 0 or larger than 30.

is there a difference between using a timer and using
startreceiverat in terms of process life? i've tried both, and
except for moving some widget update code around a little,
both versions appear to behave the same. or to put it differently,
is startreceiverat the "correct" way?
There is no real relation between the two. StartReceiverAt will cause the process to start, assuming the OS doesn't deny it. If you try it with a too short interval the OS will ignore it.
Timer will never start the process.
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
i don't understand how
my widget runs for hours without needing startreceiverat to update it.
I don't know if it applies to you also, but my widget (which I haven't updated to receivers) run on Android 12 because I happen to have a service with an active notification all the time.
This seems to keep the process alive and running as it was under Android 11
 
Upvote 0
Top