Reputation: 37577
im making an Android app, i have a service, that update my local DB in background, by downloading data from the remote DB.
i have to put a thread on the service, because i dont know why, when i use a simple handle style bucle on the service it frozen my app during some secs when it is updating the local db. (i have my local db in a dbAdapter on MyApplication class)
OK, then i put a thread on the service, but i dont know why, if i start the service, the thread of the service is frezzing my APP :S. it's suposed that when u use services and threads code is executed in background and doesnt froze nothing, but in this case is frezzing my app. ¿how to avoid it?
this is the code of my service:
public class MyServiceLocalDB extends Service implements Runnable{
RemoteConnection con; //conexion remota
//para almacenar la config local de mi app
static SharedPreferences settings;
static SharedPreferences.Editor configEditor;
boolean serviceStopped;
private static MyDbAdapter mDb;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
serviceStopped=false;
settings = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
configEditor = settings.edit();
con = new RemoteConnection();
mDb = new MyDbAdapter(this);
mDb.open();
}
@Override
public void onDestroy() {
//player.stop();
serviceStopped=true;
}
@Override
public void onStart(Intent intent, int startid) {
//player.start();
this.run();
}
public void updateDB()
{
mDb.clearDB();
List<Friend> myFriends=con.RetrieveFriends(settings.getString("login",""));
List<Permission> myPermissions=con.RetrievePermissions(settings.getString("login",""));
Permission p1 = null;
for (int i=0;i<myFriends.size();i++)
{
mDb.createUser(myFriends.get(i).getEmail(),myFriends.get(i).getFullName(),myFriends.get(i).getMovilephone(),myFriends.get(i).getMovileOperatingSystem(),myFriends.get(i).getPermission());
//p1=con.RetrievePermissionWithUser("[email protected]", myFriends.get(i).getEmail());
}
for (int i=0;i<myPermissions.size();i++)
{
p1=myPermissions.get(i);
String hour1=formatHourFromTime(p1.getHour1());
String hour2=formatHourFromTime(p1.getHour2());
mDb.createPermission(p1.getFk_email1(),p1.getFk_email2(),""+p1.getValidated(),hour1,hour2,p1.getDate1Formated(),p1.getDate2Formated(),""+p1.getWeekend(),p1.getFk_type());
p1=null;
}
//MyApplication.getDatabaseAdapter().clearDB();
MyApplication.setDatabaseAdapter(mDb);
}
public String formatHourFromTime(Time time)
{
String hour1;
if (time.getHours()<10)
hour1="0"+time.getHours();
else
hour1=""+time.getHours();
if (time.getMinutes()<10)
hour1=hour1+":0"+time.getMinutes()+":00";
else
hour1=hour1+":"+time.getMinutes()+":00";
return hour1;
}
public void run() {
while (serviceStopped==false)
{
//handler.sendEmptyMessage(0);
try {
Thread.sleep(25000);// sleeps
} catch (InterruptedException e) {}
updateDB();
}
}
}
Upvotes: 1
Views: 1246
Reputation: 28418
The onStart
is called by the OS on a main UI thread, that's why you got stuck there (you block the main UI thread in the run()). Instead of this.run();
you should start a new Thread here - new Thread(this).start();
.
BTW, onStart
is deprecated. Implement onStartCommand
instead.
Upvotes: 5