机器人 - 如何在不同的线程比主线程中运行服务? [英] android-How to run Service in different thread than main thread?

查看:228
本文介绍了机器人 - 如何在不同的线程比主线程中运行服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个Android的应用程序,由服务来读取多个小时的传感器值。当我启动服务我的设备得到挂起,所有的其他进程得到缓慢。为了解决这个问题,我已经尝试在不同的线程startservice下面给出,但问题依然存在。

I am trying to develop a application in android that consists a service to read the sensor value for multiple hours. When i start the service my device get hang and all the other process is got slow. To solve this problem i have try to startservice in separate thread as given below but problem is still there.

    new Thread(new Runnable() {

                @Override
                public void run() {
                    Intent intent=new Intent(getApplicationContext(), SensorService.class);
                    startService(intent);

                }
            }).start();

本线程才开始在主线程不同的线程,但服务运行的服务。 PLZ有人帮助我如何在单独的线程中运行的服务?

this thread only start the service in different thread but service run in main thread. Plz someone help me how to run service in separate thread ?

推荐答案

应用程序组件(服务,活动等)总是在主线程中运行,无论什么线索,他们是从启动。 考虑入手线程在你的服务,或使用 IntentService

Application components (services, activities, etc) always run in main thread, no matter what thread they are started from. Consider starting thread in your Service instead, or use an IntentService.

在您的特定情况下,你可能会尝试注册一个全球性的的BroadcastReceiver 传感器的变化,这反过来会启动 IntentService 将在数据库等新收购的价值。

In your particular case you might try to register a global BroadcastReceiver for sensor changes, which, in turn,will start an IntentService to put newly acquired values in db, etc.

其实,这里是链接,以解决类似的问题。

Actually, here is the link to similar question solved.

再次,这是不是一个真正的多线程问题。整个任务必须执行的其他方式。

Again, this is not really a multithreading issue. The whole task must be implemented the other way.

这篇关于机器人 - 如何在不同的线程比主线程中运行服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆