运行多个服务 [英] Running multiple services

查看:98
本文介绍了运行多个服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建设的一些运动检测功能集成到我的应用程序。我还没有发现的持续监控加速度计不保持清醒的手机所有的时间的方式。在试图解决这个问题,我现在有一个服务实现SensorEventListener。我可以启动该服务的时间间隔(使用报警管理器),从设备acclerometer读数,确定该装置正在移动,然后关闭该服务。一般情况下,这似乎如下:

 公共类的MyService延伸服务实现SensorEventListener {

    @覆盖
    公共无效的onCreate(){
        super.onCreate();
    }

    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
        super.onStartCommand(意向,标志,startId);
                / *注册这个SensorEventListener与Android传感器服务* /
        返回START_STICKY;
    }

    @覆盖
    公众的IBinder onBind(意向意图){
        返回null;
    }

    @覆盖
    公共无效的onDestroy(){
        / *注销SensorEventListener * /
        }

    / * SensorEventListener实施************************ /

    @覆盖
    公共无效onAccuracyChanged(传感器传感器,诠释精度){}

    / *接收回调时,传感器值的变化* /
    @覆盖
    公共无效onSensorChanged(SensorEvent事件){
        / *确定是否移动和放大器;然后调用stopSelf()关闭服务下来* /
    }
}
 

我有进一步的问题,但是,在我的申请具有的是,不同的时间表调用第二服务。据我所知,这两个服务都将在同一个线程,这是不好的,因为它们可能发生冲突运行。

我需要知道,如果有一种安全的方式在同一应用程序中同时运行多个服务。我曾考虑使用标准的服务类IntentService代替。我知道,这些实施自己的工作线程来处理调用。这个问题存在,我不知道我怎么可以实现从一个IntentService内需要SensorEventListener异步回调的类型。为了把它的另一种方式,我也不能保证像onSensorChanged的方法将收到一个回调IntentService完成其工作,并关闭之前。

这个问题上的解决方法的任何建议都非常AP preciated。

解决方案
  

因为它们可能会出现冲突。

如何?

  

我需要知道,如果有一种安全的方式在同一应用程序中同时运行多个服务

本身并无问题在同一应用程序内多个服务同时具有,任何超过有在具有相同的应用一个以上活动的特性的问题。

  

这个问题上的解决方法的任何建议都非常AP preciated。

您还没有表现出有问题。

I'm currently building some movement-detection functionality into my application. I haven't found a way of continually monitoring the accelerometer without keeping the phone awake all the time. In trying to overcome this, I currently have a Service implementing SensorEventListener. I can start this service at intervals (using alarm manager), get readings from the device acclerometer, determine if the device is moving, and then shut down the service. Generally, this appears as follows:

public class MyService extends Service implements SensorEventListener {

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
                /* Register this SensorEventListener with Android sensor service */
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        /* Unregister SensorEventListener */
        }

    /* SensorEventListener Implementation ************************/

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {}

    /* Receives callback when sensor values change */
    @Override
    public void onSensorChanged(SensorEvent event) {
        /* Determine if moving & then call stopSelf() to shut service down */
    }
}

I have a further problem, however, in that my application has a second service which is invoked on a different schedule. As far as I know, both of the services will be run in the same thread, which is not good as they could conflict.

I need to know if there is a safe way to run more than one service simultaneously within the same application. I have considered the use of IntentService instead of the standard Service class. I am aware that these implement their own worker thread for handling invocations. The problem there is that I have no idea how I can implement the type of asynchronous callbacks required by SensorEventListener from within an IntentService. To put it another way, I have no guarantee that a method like onSensorChanged will receive a callback before IntentService completes its work and shuts down.

Any suggestions on a workaround to this problem are highly appreciated.

解决方案

as they could conflict.

How?

I need to know if there is a safe way to run more than one service simultaneously within the same application

There is no intrinsic problem in having more than one service simultaneously within the same application, any more than there is an intrinsic problem in having more than one activity in the same application.

Any suggestions on a workaround to this problem are highly appreciated.

You have not demonstrated a problem.

这篇关于运行多个服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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