如果这个处理程序(可运行)在服务上启动,为什么会减慢我的应用程序的速度?
我正在使用后台服务,它正在检索数据并在远程服务器上插入数据。好的,我将其放在后台服务上,因为我想在后台完成此操作而不减慢我的应用程序的速度,但它减慢了我的应用程序的速度!
正如您将在代码中看到的那样,它的睡眠时间为 60 秒,而我的应用程序每 60 秒就会冻结 2/3 秒,我确信这就是这段代码,但我不知道如何解决它
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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使您创建了一个服务,也不意味着它将在单独的线程上运行。看看 http://developer.android.com/reference/android/app/服务.html
请花一些时间阅读 Android 中服务的实际工作原理 http://developer.android .com/guide/topics/fundamentals/services.html
因此,
IntentService
和计划警报可以成为这里的解决方案。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
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.