[android-beginners] Re: problem with service

2009-08-28 Thread Teal

Thanks for the useful explanation, Lutz.  This might be a newbie
question, but if you're spawning a new thread anyway, why run it in a
Service?  Why not just spawn from Activity?  Thanks.

On Aug 28, 6:25 am, Lutz Schönemann
 wrote:
> A service is not running in it's own thread by default. It uses the  
> main thread (same thread as your activity). It is a little confusing  
> if you look at the lifecycles of a service and an activity so you  
> might think that it is running in the "background" by default.
>
> What you should do is writing a function that starts a thread for each  
> download you want to do. This can look like:
>
> class YourService extends Service {
>
>         /**
>          * class that will be used to create a new thread
>          */
>         class Downloader implements Runnable {
>                 private Uri uri;
>
>                 public void setUri(Uri uri) {
>                         this.uri = uri;
>                 }
>
>                 public void run() {
>                         // code to download file
>                 }
>         }
>
>         // inside your method start a new thread:
>         private void startDownloadThread(Uri uri) {
>                 Downloader d = new Downloader();
>                 d.setUri(uri);
>                 Thread t = new Thread(d);
>                 t.start();
>         }
>
> }
>
> I havn't tested this code so maybe you have to play around a little to  
> get it work
>
> Am 28.08.2009 um 00:26 schrieb Justin Anderson:
>
>
>
> > Some source code might help...
>
> > --
> > There are only 10 types of people in the world...
> > Those who know binary and those who don't.
> > --
>
> > On Wed, Aug 26, 2009 at 4:56 AM, Imran  wrote:
>
> > Hey All,
>
> >      I have a service which should download files in background, i am
> > calling this service form another activity,
> >      the problem is when i am calling this service form another
> > Activity the present Activity is getting Freezed( i.e not able to get
> > control back ) until the all files in service are downloaded.
>
> >  once all the files in the service are downloaded the activity is
> > getting the control back.
>
> > what i want is the the service should download all the files in
> > background with effecting the Activity which called the service.
>
> > Help me Guys..!!
>
> > Thanks,
> > Imran- Hide quoted text -
>
> - Show quoted text -
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---



[android-beginners] Re: problem with service

2009-08-28 Thread Lutz Schönemann

A service is not running in it's own thread by default. It uses the  
main thread (same thread as your activity). It is a little confusing  
if you look at the lifecycles of a service and an activity so you  
might think that it is running in the "background" by default.

What you should do is writing a function that starts a thread for each  
download you want to do. This can look like:


class YourService extends Service {

/**
 * class that will be used to create a new thread
 */
class Downloader implements Runnable {
private Uri uri;

public void setUri(Uri uri) {
this.uri = uri;
}

public void run() {
// code to download file
}
}

// inside your method start a new thread:
private void startDownloadThread(Uri uri) {
Downloader d = new Downloader();
d.setUri(uri);
Thread t = new Thread(d);
t.start();
}

}


I havn't tested this code so maybe you have to play around a little to  
get it work


Am 28.08.2009 um 00:26 schrieb Justin Anderson:

> Some source code might help...
>
> --
> There are only 10 types of people in the world...
> Those who know binary and those who don't.
> --
>
>
> On Wed, Aug 26, 2009 at 4:56 AM, Imran  wrote:
>
> Hey All,
>
>  I have a service which should download files in background, i am
> calling this service form another activity,
>  the problem is when i am calling this service form another
> Activity the present Activity is getting Freezed( i.e not able to get
> control back ) until the all files in service are downloaded.
>
>  once all the files in the service are downloaded the activity is
> getting the control back.
>
> what i want is the the service should download all the files in
> background with effecting the Activity which called the service.
>
> Help me Guys..!!
>
> Thanks,
> Imran
>
>
>
>
> >


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---



[android-beginners] Re: problem with service

2009-08-27 Thread Justin Anderson
Some source code might help...

--
There are only 10 types of people in the world...
Those who know binary and those who don't.
--


On Wed, Aug 26, 2009 at 4:56 AM, Imran  wrote:

>
> Hey All,
>
>  I have a service which should download files in background, i am
> calling this service form another activity,
>  the problem is when i am calling this service form another
> Activity the present Activity is getting Freezed( i.e not able to get
> control back ) until the all files in service are downloaded.
>
>  once all the files in the service are downloaded the activity is
> getting the control back.
>
> what i want is the the service should download all the files in
> background with effecting the Activity which called the service.
>
> Help me Guys..!!
>
> Thanks,
> Imran
>
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---