如果这个处理程序(可运行)在服务上启动,为什么会减慢我的应用程序的速度?

发布于 2024-11-13 06:29:53 字数 1860 浏览 3 评论 0原文

我正在使用后台服务,它正在检索数据并在远程服务器上插入数据。好的,我将其放在后台服务上,因为我想在后台完成此操作而不减慢我的应用程序的速度,但它减慢了我的应用程序的速度!

正如您将在代码中看到的那样,它的睡眠时间为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱人如己 2024-11-20 06:29:53

即使您创建了一个服务,也不意味着它将在单独的线程上运行。看看 http://developer.android.com/reference/android/app/服务.html

请注意,与其他应用程序对象一样,服务在其托管进程的主线程中运行。这意味着,如果您的服务要执行任何 CPU 密集型(例如 MP3 播放)或阻塞(例如网络)操作,它应该生成自己的线程来完成该工作。有关这方面的更多信息可以在进程和线程中找到。 IntentService 类可作为 Service 的标准实现,它有自己的线程,可以在其中安排要完成的工作。

请花一些时间阅读 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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文