Activity blocks till service completes

by abhi » Mon, 12 Oct 2009 02:46:05 GMT


Sponsored Links
 Hi,

I am starting a service from an activity. Once the services starts,
the calling activity loses focus and blocks till the service is
completed. Why is this and is there a way around it to return the
control to the calling activity while the service runs in the
background?

Thanks,

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



Activity blocks till service completes

by Dianne Hackborn » Mon, 12 Oct 2009 02:56:16 GMT


 Services don't take focus input focus -- a service doesn't cause the current
window to lose input focus, nor the foreground activity to be paused.
If you mean the thread of your activity isn't running, then you may just be
doing all your work in the service on the main thread.  Please note that a
service's callbacks (but not necessarily calls through any IBinder interface
it publishes) happen on the main thread.  If you want to run on another
thread, you'll need to make it yourself.






-- 
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, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

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


Sponsored Links


Activity blocks till service completes

by abhi » Tue, 13 Oct 2009 02:19:45 GMT


 I created a new thread and tried starting the service from there, but
the service is not being started from the new thread. When I set a
break point in the new thread, it does not hit and there is nothing
updated on logcat. Any ideas as to why this is happening?

Thread th = new Thread(new ServiceStarter());
th.start();

In the ServiceStarter, class I am creating the service and starting
it:

class ServiceStarter extends Activity implements Runnable
{

        @Override
        public void run()
        {
                try
                {
                        Intent service = new Intent
(ServiceStarter.this,Alarm.class);
                        startService(service);

                }
                catch(Exception ex)
                {
                        ex.printStackTrace();
                }
        }
}


My service looks like this:

public class MyService extends Service
{

        @Override
        public IBinder onBind(Intent intent)
        {
                return null;
        }

        @Override
        public void onCreate()
        {
                super.onCreate();
        }

        @Override
        public void onStart(Intent intent, int startId)
        {
                boolean loopVar = true;

                while(loopVar == true)
                {
                         //Do stuff
                        if(condition)
                        {
                                //Exit the loop
                                loopVar = false;
                        }
                }
        }
}




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



Activity blocks till service completes

by Mark Murphy » Tue, 13 Oct 2009 02:38:32 GMT


 


No, you start the thread *in* the service. All Android components are
started on the "UI thread". If you want a component to use a background
thread, that component has to start up the background thread.

Though I really recommend AsyncTask over starting your own threads, in
most cases.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

Android App Developer Books:  http://commonsware.com/books.html 

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



Activity blocks till service completes

by Dianne Hackborn » Tue, 13 Oct 2009 02:38:46 GMT


  don't see you creating a service anywhere in your code service, so I'm not
sure what you mean about creating one and not having it run... yes, if you
don't create one for your service there won't be one for it to run. :}

You need to make a thread in your service -- either in onCreate() or
onStart() -- in which to do your work, and quit the thread at the
appropriate time when done.

Or you really may want to consider just using IntentService to let it take
care of this for you:

http://developer.android.com/reference/android/app/IntentService.html

On Mon, Oct 12, 2009 at 11:19 AM, abhi <rkabhi1...@gmail.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, and so won't reply to such e-mails. All such
questions should be posted on public forums, where I and others can see and
answer them.

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



Activity blocks till service completes

by abhi » Tue, 13 Oct 2009 05:41:05 GMT


 hen I created the thread in the Service itself, it worked. Thank you
very much!

On Oct 12, 12:37pm, Dianne Hackborn <hack...@android.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Is there any guides for porting android into x86?

Dear friends:
        Is there any doc exist for guiding porting android into x86?
Such as todo lists, how to do,what to do? Thanks!

--~--~---------~--~----~------------~-------~--~----~
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: 

2. Is there any version exist that can be directly used in x86 based netbooks

HI Friends:
        I'm a fresh guy on Android system. And have the following
questions:
        1. Can Android 1.5 be directly used on X86 based netbooks? I
heard that there's a project named "Asus eee701", but in the branch of
"Cupcake", I don't konw if "Cupcake" means the same for
"Android",what's the  difference? If I use repo init -u
git://android.git.kernel.org/platform/manifest.git -b android-1.5 to
download the packages, can it works?
        2.Further more,Android's mediaframe work OpenCore's current
version is V2.02, is there any formal release than can be directly in
x86 based netbooks or laptops. I want to add more codecs into it.so
need to get the code that fit x86 architecture and can be easily
built.Is cupcake1.5 fit this?
        Thanks in advance!

--~--~---------~--~----~------------~-------~--~----~
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: 

3. Android on HTC Alpine

4. gl functions not working, egl functions are working - android port to Overo TI OMAP 3503

5. How can i get the string from edittext?

6. Creating and showing a view/window from a Service

7. Textview Scrolling