Android的意图服务实施SensorEventListener [英] Android Intent Service implementing SensorEventListener

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

问题描述

我有一个关于IntentService Android中的问题。我如下定义自己的服务:

I have a question about IntentService in Android. I define my own service as below:

公共类ABC扩展IntentService实现SensorEventListener {

public class ABC extends IntentService implements SensorEventListener {

@覆盖
  保护无效onHandleIntent(意向意图){}

@Override protected void onHandleIntent(Intent intent) { }

@覆盖
  公共无效onSensorChanged(SensorEvent事件){}
}

@Override public void onSensorChanged(SensorEvent event) { } }

现在,如果我开始从其他活动服务,onHandleIntent()被调用。文件说,它会启动一个工作线程来处理请求。然而,当onSensorChanged()方法被调用时,哪个线程将执行在onSensorChanged定义()方法code。这将是工作线程或主应用程序线程???

Now if i start the service from other activity, onHandleIntent() is invoked. Documentation says it launches a worker thread to process the request. However, when onSensorChanged() method is invoked, which thread will be executing the code defined in onSensorChanged() method. Will it be the worker thread or the main application thread???

推荐答案

按照<一个href=\"https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/app/IntentService.java\"相对=nofollow> IntentService源$ C ​​$ C ,IntentServices停止自己,当他们不再有任何消息(即 stopSelf 调用)。因此,如果你想有一个长期运行的服务(我假设是适合你的 SensorEventListener ),那么你应该使用普通的服务

Per the IntentService source code, IntentServices stop themselves when they no longer have any messages (the stopSelf call). Therefore if you want a long running Service (which I assume would be appropriate for your SensorEventListener), then you should use a regular Service.

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

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