service start and ui hanging

by Bob » Fri, 13 Mar 2009 23:05:18 GMT


Sponsored Links
 Hi,
I have moved some intensive processing from my main thread to a
service.  My UI is hanging and then crashing when I start the service,
even if I put the service start in a new thread started via

 Thread updateThread = new Thread(null, backgroundRefresh2,
"new_thread");
                  updateThread.start();


What am I doing wrong?  Also, it seems like the cause of the eventual
crash in the service/worker thread doesn't show up in LogCat.

Thanks,
Bob

--~--~---------~--~----~------------~-------~--~----~



service start and ui hanging

by Streets Of Boston » Fri, 13 Mar 2009 23:32:20 GMT


 Hi Bob,

Your code snippet is not enough to give you some more info.
E.g. how does backgroundRefresh2 looks like (it public void run()
implementation).

Based on its name 'backgroundRefresh2': does it access View-s and
modify these view (e.g. update text-view, images, etc.)?
If so, that may explain your crash. You should not access any View (or
subclass of View) in any other thread than your main message-
dispatching thread (i.e. the thread on which your onCreate/onDestroy/
onPause/onResume/on<anything> is called).




--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


service start and ui hanging

by Bob » Sat, 14 Mar 2009 00:39:22 GMT


 Thanks for your quick response.  It doesn't access or modify any
views.  It accesses the context to read in some raw resources and
writes to the sqllite database also via context.




--~--~---------~--~----~------------~-------~--~----~



service start and ui hanging

by Dianne Hackborn » Sat, 14 Mar 2009 01:03:55 GMT


 At the very least, you should include (and look at!) the stack crawl of the
crash.  That will usually tell you all you need to know about why it
crashed.  The stack crawl is in the log.








-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~---------~--~----~------------~-------~--~----~



service start and ui hanging

by andrew » Sat, 14 Mar 2009 02:10:59 GMT


 Thanks for your quick response.  It doesn't access any other views.
It does access the context object so as to work with the sqlite dbs.

On Mar 13, 10:32am, Streets Of Boston <flyingdutc...@gmail.com>




--~--~---------~--~----~------------~-------~--~----~



service start and ui hanging

by Bob » Sat, 14 Mar 2009 02:16:57 GMT


 Thanks, I'll look more at the logs for crash issue.  But regardless of
the crash, am I correct in thinking that if I call a service it should
do its processing in the background and the main UI thread should
immediately update?  For this does it matter whether I start the
service from a background thread or not?  The crash doesn't occur for
a while into the processing but in the mean time my UI is not
progressing.






--~--~---------~--~----~------------~-------~--~----~



service start and ui hanging

by Dianne Hackborn » Sat, 14 Mar 2009 02:57:01 GMT


 m...  it sounds like you are just doing a lot of work in the main thread,
and getting an ANR dialog. Don't do that. :) If you have lots of work to
do, do it in a background thread. All Service callbacks happen on the main
thread.

On Fri, Mar 13, 2009 at 11:16 AM, Bob <bshumsk...@yahoo.com> wrote:



--
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support. All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~---------~--~----~------------~-------~--~----~



service start and ui hanging

by Bob » Sat, 14 Mar 2009 03:22:57 GMT


 gt;> If you have lots of work to do, do it in a background thread.

Thanks, I thought I running on a different thread by calling the
service from a background thread. Have I done this incorrectly or
should I not be using the context object?

public void openInitialThread() {
Thread updateThread = new Thread(null, backgroundRefresh2,
"new_thread");
updateThread.start();
}

private Runnable backgroundRefresh2 = new Runnable() {
public void run() {
ComponentName service=_context.startService(new Intent
(_context,InitialContentService.class));

}
};

On Mar 13, 12:56pm, Dianne Hackborn <hack...@android.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



service start and ui hanging

by Mark Murphy » Sat, 14 Mar 2009 03:45:19 GMT


 


Personally, I prefer the pattern of starting the service on the UI
thread but then having the service immediately start up a background
thread to do its work. That way, the service encapsulates the thread --
the client does not need to know or care what the service's threading
model is.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
Android Training in Sweden --  http://www.sotrium.com/training.php 

--~--~---------~--~----~------------~-------~--~----~



service start and ui hanging

by Dianne Hackborn » Sat, 14 Mar 2009 04:51:40 GMT


 tartService() tells the system to start the service.  The Service callbacks
are still made on the main thread, as always.

On Fri, Mar 13, 2009 at 12:22 PM, Bob <bshumsk...@yahoo.com> wrote:



--
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support. All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~---------~--~----~------------~-------~--~----~



service start and ui hanging

by Bob » Sat, 14 Mar 2009 07:49:01 GMT


 'm sorry, I don't understand what this means.  I have no callbacks.
The service inserts some data into the filesystem and then exists.
How do I this so the UI won't hang while the processing is going on?

On Mar 13, 2:51pm, Dianne Hackborn <hack...@android.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



service start and ui hanging

by Dianne Hackborn » Sat, 14 Mar 2009 07:57:28 GMT


 ll of the callback methods on Service, such as onStart(), happen in the
main UI thread. If you have a long job to do, use the Thread class to do it
in another thread. Semantically this is the same as the Activity callbacks.

On Fri, Mar 13, 2009 at 4:48 PM, Bob <bshumsk...@yahoo.com> wrote:



--
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support. All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Error in the emulator

Hello All!

I have been programming a bit in Android, and I was doing some tests.
And well, now I see in the emulator, when I finish some execution of a
APP that it's comes to write "=" all the time (I don't know why). So
It tries to search it on the net, and that's the second problem. It
before this error it was be able to connect and show pages on the
emulator, but now NO, it's the wifi turn off and the 3g too. So I
don't know what I have done but I want to take back to the first
state. Even I have tried to use the WebViews, but I think they are
giving me a error because it doesn't have connection.

Finally, I have tried to reinstall the SDK 2.0.1 but it has not
worked. Thank you all!

--

2. Android 2.0 - Dim screen programmatically

I am wanting to dim the screen of a device from a widget.  Basically
the same way the Power Manager widget for Android 2.0 does.  I have
tried this...
________________________________________

Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, someIntValue);
________________________________________


I have this that works from an application window....
________________________________________

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = (float) (someIntValue/ 255.0);
getWindow().setAttributes(lp);
________________________________________

But this doesn't work from the widget.  Any idea on how to dim the
screen from a widget?

--

3. open archive file

4. Andorid Set a new QUICK_LAUNCH in android.provider.Settings

5. GK: How to recognize Left touch dragging and Right touch dragging(Scrolling) in Android?

6. Using Google Maps API for a navigational app

7. Code obfuscator