1

In my Android Application, I'm running a service who slows a lot my application. I don't know if there's a best way that the one I choose. I think it's because I use the sleep() function to check every x seconds if something changes on a website but I'm absolutely not sure about this. Here's the code:

    package com.example.goo;

//import

    public class ServiceLive extends Service{

        ThreadDemo td = new ThreadDemo();

        String[] tabAllTeams;
        String[] tabAllScores;
        String code;
        Document doc;

        @Override
        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;
        }

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



            System.out.println("Lancement de mon service");
            td.run();
        }

        public String[] getAllTeams(){
            return tabAllTeams;
        }

        public String[] getAllScores(){
            return tabAllScores;
        }

        private class ThreadDemo extends Thread{
            @Override
            public void run() {
                super.run();
                try{

                    ThreadDemo.sleep(3000*10);
                    System.out.println("check again");
                    new NetworkOperation().execute();
                }catch(Exception e){
                    e.getMessage();
                }
                new NetworkOperation().execute();
            }
        }

        //Get All Data
        class NetworkOperation extends AsyncTask<Void, Void, String > {
            protected String doInBackground(Void... params) {
                try {
                    //Définir Site De Récupération
                    doc = Jsoup.connect("http://www.siteduzero.com").get();
                    //Définir Classe de Récupération
                    Elements getId = doc.getElementsByClass("page-visitors");   
                    code = getId.text();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                td.run();
                return null;
            }
        }
    }

What's the part that slows the app ? Thank's

SOLUTION:

Check the answer of Maurice Gavin here: How to start service in new thread in android

Community
  • 1
  • 1
qwertzuiop
  • 685
  • 2
  • 10
  • 24
  • 1
    Set some breakpoints and see how long it takes between method calls, allocations and such. – ChiefTwoPencils Jul 17 '13 at 08:30
  • 1
    Read about IntentService (http://developer.android.com/reference/android/app/IntentService.html). I think it will do what you want. Otherwise you must use AsyncTask OR your own Thread. – Alexander Mikhaylov Jul 17 '13 at 08:54

2 Answers2

1

You are running the Thread on the main thread of the service. The service's main thread is the same thread as the UI thread in your activity..

Therefor the Sleep call is running on your UI thread.....

Please start the Thread with .start() instead of .run()

Wiht .run() you will start the runnable inside the Thread, not the thread itself...

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

        System.out.println("Lancement de mon service");
        td.start();
    }
private class ThreadDemo extends Thread{
    @Override
    public void run() {
        super.run();
        try{

            ThreadDemo.sleep(3000*10);
            System.out.println("check again");
            doInternetStuff();
        }catch(Exception e){
            e.getMessage();
        }
        doInternetStuff();
    }
}

private void doInternetStuff() {
    try {
        //Définir Site De Récupération
        doc = Jsoup.connect("http://www.siteduzero.com").get();
        //Définir Classe de Récupération
        Elements getId = doc.getElementsByClass("page-visitors");   
        code = getId.text();
    } catch (Exception e) {
        e.printStackTrace();
    }
    new ThreadDemo().start();
}
Ion Aalbers
  • 7,830
  • 3
  • 37
  • 50
  • This will not work, because you must call AsyncTask.execute() from ui thread. If he will start thread and try to call AsyncTask.execute in it, then he will get an exception. – Alexander Mikhaylov Jul 17 '13 at 08:54
  • That is true, the ASyncTask shouldn't be used, that stuff could also be executed inside the thread.. – Ion Aalbers Jul 17 '13 at 09:03
  • Edited, now it should work. You dont have to use a seperate AsyncTask – Ion Aalbers Jul 17 '13 at 09:34
  • Thank's I'm gonna try it – qwertzuiop Jul 17 '13 at 09:38
  • I tried your code and it was still very slow, BUT I found a solution to my problem. The solution of Maurice Gavin (http://stackoverflow.com/questions/5138265/how-to-start-service-in-new-thread-in-android ) works perfectly. Thank's for your answer – qwertzuiop Jul 17 '13 at 12:22
1

Your problem is exactly here:

public void onCreate() {
        super.onCreate();
        System.out.println("Lancement de mon service");
        td.run();
}

A service does not run on its own thread but instead on your application main thread. Moreover run does not start a new thread but runs the code of the Runnable object inside the thread which is making the call. To conclude you're doing all your work in the UI thread. First of all you should call td.start() and I would also put this code in onStartCommand() and not onCreate(). onCreate() is called once everytime the service is created but not everytime it receives an Intent. onStartCommand() instead runs for every Intent received by the Service.

type-a1pha
  • 1,891
  • 13
  • 19