No, calling Thread.sleep() won't work.
Android framework is largely single-threaded, event-driven.
This means that your application and the framework run on the same
thread, passing control to each other, doing work in small pieces. This
thread is called the UI thread, and blocking it by calling sleep() can
do only one thing - cause the "Application Not Responding" dialog to appear.
The right thing to do is call bindService, and return from onResume.
You've done your piece of work (responded to onResume, and requested
that Android bind a service). Now you need to give Android a chance to
do its piece of work - by returning from onResume into Android framework
code, which will start the service (if necessary) and bind it, notifying
your callback.
Then it's your turn again - once in the ServiceConnection callback, you
know the service has been bound, and you can talk to the service and
ultimately populate the UI.
So that's basically the scheme with services.
You might also want to look at ContentProviders. They have a few
advantages over Services for this case - their lifecycle is managed by
Android, access is synchronous (using ContentResolver), and they handle
propagating data changes to existing queries / cursors (so if you have a
ListView, its data will be "live").
-- Kostya
06.08.2010 12:52, Bender пишет:
I tried the following in my activity:
mServiceConnection = new
DbServiceConnection(mDatabaseBinder);
final Intent databaseServiceIntent = new Intent(this,
DatabaseService.class);
this.bindService(databaseServiceIntent, mServiceConnection,
Context.BIND_AUTO_CREATE);
while(mDatabaseBinder == null) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// catch...
}
}
Did you mean that? Now it should wait until the mDatabaseBinder is set
which should be in the onServiceConnected() method but that code
results in an endless loop, mDatabaseBinder stays null. Maybe I got it
wrong how the components work together. As far as I understood it, you
have a service running in the background, which returns a binder in
onBind(). The service connection "fills" the binder
onServiceConnected() so it can be used in the activity to access the
services variables. Is that wrong?
On 6 Aug., 10:26, Kostya Vasilyev<[email protected]> wrote:
Sure. The service connection callback you seem to already have in your code.
--
Kostya Vasilyev --http://kmansoft.wordpress.com
--
Kostya Vasilev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com
--
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
ATTENTION: Android-Beginners will be permanently disabled on August 9 2010. For
more information about this change, please read [http://goo.gl/xkfl] or visit
the Group home page.
Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en