Friday, June 24, 2011

[android-developers] Re: Static boolean in Service "resets" after device comes back to sleep?

After reading my text I just realized something!

in the service onStartCommand() i check if the passed intent is !=
null and if that is the case proceed to check the connection state.
But if the service was killed while in "idle" the intent would have
been null and i would NOT have proceeded to check the connection state
and the static boolean would now be false (senes the service has been
re-created). I made fix so that if(intent != null) { doStuff(); } else
{ checkConnection();}. Now if the service is killed and later
restarted it would check for the connection!

// Fredrik

On Jun 24, 3:18 pm, Freddman <fredd...@gmail.com> wrote:
> I have a Service that is launched from in a widget, when the service
> start's it sets a static boolean to either true or false depending if
> it's able to connect to a device in my home network.
>
> I have different buttons on my widget performing different task if we
> are connected, I also have an alarm that triggers an update at
> different intervals ( it sends a specific Intent to the widget ).
> Everything works fine, but if I leave the phone long enough it goes
> into sleep and when I return the buttons won't send any actions to the
> device because for some reason the "connected" boolean has changed
> from true to false.
>
> The strange thing is that if we are unable to connect the device, it
> prints out "Unable to connect" in the widget. But in this case it
> doesn't but the boolean has still changed to false.
> Example:
>
> if (mConnection.getConnection() != null) {
>                                         CONNECTED = true;
>                                         doStuff();
>                                 } else {
>                                         CONNECTED = false;
>                                         updateWidget("Unable to connect");
>                                         stopSelf();
>
> }
>
> So if the Service has been killed, and is re-launched and unable to
> connect the widget would have said "Unable to connect" instead shows a
> message that has not been valid for some minutes (and should have
> changed when the automated update triggers). I know my
> onClickListeners work because one button doesn't send a command but
> instead opens up an Activity, and that still works.....
>
> Any ideas for this strange behaviour?

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment