Reputation: 37637
i am using a background service that it is retrieving data and inserting data on a remote server. OK, i puted it on a background service because i wanted to get that done in background without slowing down my app, but it is slowing down my app !
as you will see in the code, it haves a sleep of 60 seconds, and my app is getting frozen 2/3 seconds each 60 seconds, it is this code, i am sure, but i dont know how to solve it
public class MyService extends Service implements Runnable{
boolean serviceStopped;
RemoteConnection con; //conexion remota
List <Position> positions;
static SharedPreferences settings;
static SharedPreferences.Editor configEditor;
private Handler mHandler;
private Runnable updateRunnable = new Runnable() {
@Override public void run() {
//contenido
if (serviceStopped==false)
{
positions=con.RetrievePositions(settings.getString("login","")); //traigo todas las posiciones
if (positions.size()>=10) //si hay 10 borro la mas vieja
con.deletePosition(positions.get(0).getIdposition());
if (settings.getString("mylatitude", null)!=null && settings.getString("mylongitude", null)!=null)
con.insertPosition(settings.getString("mylatitude", null),settings.getString("mylongitude", null), formatDate(new Date()), settings.getString("login",""));
}
queueRunnable();//duerme
}
};
private void queueRunnable() {
//mHandler.postDelayed(updateRunnable, 60000); //envia una posicion al servidor cada minuto (60.000 milisegundos es un minuto)
mHandler.postDelayed(updateRunnable, 60000);
}
public void onCreate() {
serviceStopped=false;
settings = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
configEditor = settings.edit();
positions=new ArrayList<Position>();
con = new RemoteConnection();
mHandler = new Handler();
queueRunnable();
}
Upvotes: 1
Views: 1505
Reputation: 11775
Even if you created a service it doesn't mean it will run on a separate thread. Take a look http://developer.android.com/reference/android/app/Service.html
Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. More information on this can be found in Processes and Threads. The IntentService class is available as a standard implementation of Service that has its own thread where it schedules its work to be done.
Please take some time to read how services actually work in Android http://developer.android.com/guide/topics/fundamentals/services.html
So, IntentService
and scheduled alert can be a solution here.
Upvotes: 2