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. Linking PreCompiled Shared Object/Executable with Android RILD

Hello,

Is there any possible way, to link a precompiled shared object (*.so)
with rild. so that it can be opened using dlopen, its function can be
accessed using dlsym and finally these functions can be called using
some thread. Just like it is being done in case of reference-ril but
the only difference is that i am building my module as a separate
entitiy and after that i am trying to link its shared object with rild
while reference-ril module is built within Android.

At the moment i am trying to use following lines at the top of
Android.mk in rild folder.
include $(CLEAR_VARS)
LOCAL_PREBUILT_LIBS := my_module.so
include $(BUILD_MULTI_PREBUILT)

I have placed my_module.so in hardware/ril/rild and i can see in the
make log that it is being installed and automatically copied into out/
target/generic/system/lib/my_module.so

but when i call the following
mymodulepath = "system/lib/my_module.so"
dlopen(mymodulepath, RTLD_NOW)

it fails to open this shared object and i get the following error
using dlerror()
Cannot load library: link_image[2033]: failed to link my_module.so

Its very very urgent and i am very much new to this thing. So please
help me out guys.

Looking forward to your response and thanking in advance.

Warm Regards,
Junaid.

-- 

2. making Toast like notification?

Hello,

I have been looking into this for a while and havn't found a method to
do this, so any help will be appreciated...

I'm trying to create a custom version of the Toast notification while
keeping a similar functionality but customizing it, I have tried the
following;

Tried extending Toast and just overriding the functions I need to re
implement but thats pretty much impossible since it uses a private
class called TN that I don't access to and then the bigger problem is
the lack of access of the low level system manager it uses
ITransientNotification, WindowManagerImp, INotificationManager(one of
the big ones uses since this is queues each Toast)... anyway hit a
dead wall with that so I tried using the AlertDialog.Builder creating
a custom dialog then tried to get the windows attributes during
OnPrepareDialog/OnCreateDialog and play around with the window flags
such as LayoutParams.FLAG_NOT_TOUCHABLE and it kinda of worked but no
at the same time because the AlertDialog relies on creating a new
activity which from my understand is another dead wall since there
can't be an activity running on top of another activity at the same
time.(right? please correct me if im wrong?)

So now I'm really stuck if anyone has any ideas I would really
appreciate it thanks

-- 

3. Small, Normal, Large and xlarge. How to handle application?

4. Captured image FileNotFound

5. How to read file from assets dir?

6. Edit Bitmap Channels?

7. Hepinuyer