0

I have this class that creates threads, but I want to convert that class into Service hoping it wont be destroyed on orientation change.

Here is the class:

public class Getter {

    private final String ip;
    private final int amount, poolSize;
    private Vector<Integer> results = new Vector<Integer>();
    private final ExecutorService es;
    private Collection<Future<?>> futures = new LinkedList<Future<?>>();


    public Getter(String ip, int amount, int poolSize) {
        this.ip = ip;
        this.amount = amount;
        this.poolSize = poolSize;
        es = Executors.newFixedThreadPool(this.poolSize);

    }

    public boolean working() {
        boolean work = false;
        for (Future<?> future : futures) {
            if (!future.isDone()) {
                work = true;
            }
        }
        return work;
    }

    public Vector<Integer> getResults() {
        Collections.sort(results);
        return results;
    }

    public int threads(){
        return poolSize;
    }

    public void start() {

        for (int i = 0; i <= amount; i++) {
            futures.add(es.submit(new Get(ip)));
        }

        es.shutdown();

    }

    public void stop(){
        for (Future<?> future : futures) {
            future.cancel(true);
        }
    }

    private class Get implements Runnable { 

        private String ip;

        private Get(String ip) {
            this.ip = ip;
        }

        public void run() {
            try {
                // network stuff
                // adds result to results Vector.
            } catch (Exception ex) {

            }
        }
    }                                       
}

so is this class possible to convert into Service so it would run on background no matter what, once its started?

Rohit Malish
  • 3,209
  • 12
  • 49
  • 67

3 Answers3

0

Yes. I think this would make a good IntentService. Your next-best choice is probably AsyncTask.

More:

Generally, I would say, if the behavior of the background task is closely tied to whatever starts it (e.g., an Activity, Fragment, Service, whatever), then make it an AsyncTask. If it is self-contained and meant to serve several different components in a modular fashion, make it a Service. In either case, an AsyncTask or IntentService is very easy to make.

Sparky
  • 8,437
  • 1
  • 29
  • 41
  • any idea how complicated it is to convert my class to service? I have some experience on services but none on asynctask. And which of this two is better one to use, and which is easiest to use? – Rohit Malish Aug 16 '12 at 11:27
0

you can add this to a manifest file that stops your app getting destroyed on orientation change:

android:configChanges="orientation"

but if you want to make a service then just copy this example: http://developer.android.com/reference/android/app/Service.html you could add the class to the service and then just add an accessor to your service connection.

You can also add something to the manifest file to let your service start when the device is turned on see here Trying to start a service on boot on Android

Community
  • 1
  • 1
twigg
  • 146
  • 6
0

If you want a Service which runs "no matter what", you might want to set it as foreground.

alex
  • 10,900
  • 15
  • 70
  • 100