tags:

views:

123

answers:

2

I am writing a home screen widget following Jeff Shakey's tutorial, http://android-developers.blogspot.com/2009/04/introducing-home-screen-widgets-and.html.

This tutorial uses service to avoid any ANR timeouts. I just wonder, can I use Thread instead of service to do the work of getting data and updating RemoteViews? I don't want to create a service, because Thread is easier to handle and pass data into Thread.

Thanks.

+3  A: 

You should not rely on thread. It is not guaranteed to work. From experiences in a similar situation the thread approach worked on the emulator and the samsung galaxy but not reliable on the g1. So you should really attempt to use a service. It is quite easy: define in your manifest and startService. It is a little more cumbersome to pass data via an intent but it is manageable and the whole approach is definitely more robust.

Soulreaper
It is guaranteed *not* to work.
CommonsWare
Does it mean, thread is not working on Android or just not working for Home screen widget?What's the best practice for home screen widget, service only or service + thread?I have to load data from internet to update the widget screen, I have to avoid ANR timeouts.I tested service + thread and thread only on simulator and HTC Hero. It works fine. If the it won't work on other devices, I will give up the thread idea.
Please read up on process lifecycles:http://developer.android.com/intl/de/guide/topics/fundamentals.html#proclifeNote that if you don't have any components running in your process (foreground activities, receives in onReceive(), running services, or foreground clients of your content providers), then your process is considered by the system to be empty and may be killed at any time.
hackbod
Also you haven't said this, but please don't leave your service running the entire time someone is using your widget. This is extremely heavy-weight, and if the user has a fair number of such widgets they can easily run out of RAM to run everything. And as of Android 2.0 they can easily tell who you are that is using up their RAM, and take action like lower ratings and comments on market.
hackbod
Oh and one last thing -- Thread and Service are -completely- orthogonal concepts. They don't cover any similar functionality. If you need a thread (to do work off the main thread to avoid ANRs), you need it regardless of whether you have a service. Thus it is not uncommon to write a service that creates a thread to do its work.
hackbod
+1  A: 

I read an article saying that, to avoid ANR, service is sometimes not enough. Thread is needed.

See: http://blog.elsdoerfer.name/2009/06/03/writing-an-android-widget-what-the-docs-dont-tell-you/

"you are encouraged to use a service to perform your widget updates if you are doing anything that might take a little longer, in order to avoid Application Not Responding (ANR) timeouts. However, this will usually not be enough. ......

The solution is to have your service start a separate thread. For an example, see Jeffrey Sharkey's android-sky Widget."

Any thoughts?