android - 为什么这个处理程序(可运行)在服务上启动时会减慢我的应用程序?

标签 android handler runnable

我正在使用后台服务,它正在检索数据并在远程服务器上插入数据。好的,我把它放在后台服务上,因为我想在不减慢我的应用程序速度的情况下在后台完成它,但它正在减慢我的应用程序!

正如您将在代码中看到的那样,它有 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();
        }

最佳答案

即使您创建了一个服务,也不意味着它会在单独的线程上运行。看看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.

请花一些时间阅读服务在 Android 中的实际工作方式 http://developer.android.com/guide/topics/fundamentals/services.html

因此,IntentService 和定时提醒可以作为解决方案。

关于android - 为什么这个处理程序(可运行)在服务上启动时会减慢我的应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6212459/

相关文章:

javascript - 在 webview 中显示 android 原生日期选择器

android - 如何开始Android内核编程?

尝试中断并加入 main 时 Java 线程挂起

java - 关机还是不关机?在 ExecutorService (Java8) 中

java - callable 是否按顺序执行?

javascript - 如何在 Android Webview 中加载网页之前注入(inject) javascript?

android - 将 React Native 升级到 0.60-RC2 后找不到获取库 "libjsc.so"

java - 将数据从另一个类传递给处理程序

android - 线程和处理程序有什么区别

reference - 为什么在 AppleScript 中,不能声明对处理程序本地变量的引用?