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. Text color in fast scroll SectionIndexer box

Hi,
I have a ListView with the fast scroll enabled. The background of the
listview is white and the text is black.

The box indicating the letter of the section is being scrolled at the
moment is in black. Is it possible to customize that colore and turn
to white? If yes, how can I do that?

Thanks
Emanuele

-- 

2. Perihal Kloteran Battery Gum Plu

RR,

Berkaitan dengan kloteran Battery Gum Plus yang delay dikarenakan stock dari
pabriknya yang memang kosong, saya ingin memberitahukan bahwa barangnya baru
dikirimkan dari Taiwan pada tanggal 28 April 2010, perkiraan sampai di
Jakarta sekitar tanggal 2-3 Mei 2010.

Sebagai kompensasi dari keterlambatan ini, harga saya revisi menjadi *Rp
525.000,- *sudah termasuk ongkos kirim ke seluruh Indonesia.
Kompensasi ini juga dikarenakan harga yang diturunkan dari pihak
distributor, dan juga karena nilai dollar terhadap rupiah juga turun.

Bagi rekan2 yang sudah membayar full, silakan japri saya nomor rekeningnya
untuk pengembalian sisanya.
Terima kasih atas perhatian dan kesabarannya menunggu.

*************
Deska Affif
088-1100-GAYA (4292) | Y!: GadgetGaya
www.gadgetgaya.com  |  Light Up Your Gadget!

-- 
"Indonesian Android Community [id-android]" 

3. Sudah diralat, WTS: Motorola Titanium XT800 Heading To South Korea [Cheap Android Phone To Have 3.7-Inch Display, HDMI Out, 600MHz Processor]

4. mo tanya tentang bootloader.

5. First impressions on Droid

6. gimana ya cara hapus SuperUser WhiteList?

7. Steps to transfer a simple object from server to android using XML