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