我的IntentService在后台如何始终收听设备的震动? [英] How can my IntentService in background always listen to device's shake?

查看:94
本文介绍了我的IntentService在后台如何始终收听设备的震动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基本上是一个小部件和一项服务的应用程序。
小部件只是一个按钮,我希望它在设备的后台运行服务。
因此,用户只需单击按钮,UI中什么也没有发生,该服务就会在后台启动。
但是我希望使用IntentService实现的后台服务始终在单击小部件的按钮后在后台听设备的震动。它必须随时监控加速度传感器的变化。
最后,如果用户摇晃他的设备,我想打开某个应用程序。

I am developing an app which is basically one widget and one service. The widget is only a button which I want it to run the service in the device's background. So the user just clicks the button and nothing happens in the UI, the service starts in the background. But I want the background service, which I have implemented using IntentService, to ALWAYS listen to my device's shake in the background after the widget's button is clicked. It has to monitor the accelerometer sensor for any change at any time. Finally I want to open a certain application if the user shakes his device.

我已经实现了小部件部分,单击按钮,然后服务运行。我还实现了摇晃部分,可以摇晃并在摇晃之后做一些事情。我也知道摇动后如何打开另一个应用程序。我只是无法通过服务做到这一点!所以我的问题是这样的:

I have implemented the widget part, I click the button and the service runs. I have also implemented the shake part, I can get the shake and do some stuff after the shake. I also know how to open another application after the shake. I just can't do this through the service! So my question is this:


  • 我是否必须使我的IntentService类实现SensorEventListener?

  • 如果是,仅此实现足以使它始终监听传感器吗?那我该在onHandleIntent(Intent intent)中做什么?

  • 如果在常规Activity中实现了所有传感器更改,则将在onSensorChanged(SensorEvent sensorEvent)中监听所有传感器更改。这是否意味着我必须将所有内容都放入该函数中?

  • 我也有这个意图,它来自我的小部件到我的服务,我不知道该怎么做!我已经使用该意图启动了intentService,但是如果我不使用onHandleIntent,则我无法将setAction传递给我的意图,因此它将毫无用处!

  • Do I have to make my IntentService class implement SensorEventListener?
  • If yes, is only this implementation enough for it to listen to the sensor at all times? What should I do in onHandleIntent(Intent intent) then?!
  • All the sensor changes are going to be listened in onSensorChanged(SensorEvent sensorEvent) if it was implemented in a regular Activity. Does it mean that I have to put everything in that function? nothing in the onHandleIntent?
  • I also have this intent, comes from my widget to my service, which I don't know what to do with! I have started the intentService using that intent but if I don't use the onHandleIntent I can't pass setAction for my intent, so it would be useless!

我已经审查了以下问题:[应用程序可以随时响应],但我没有获得该服务在后台如何监听的详细信息。

I have reviewed this question: ["Shake" app to respond at any time ] but I didn't get the details of how the service listens in the background.

有人可以帮忙吗?

推荐答案

您可以同时使用 Service IntentService 。在这里,我给出了一种适合您的方法。对于 Service IntentService 之间的差异,请遵循链接。

You can use both Service and IntentService. Here I have given one of the method which should work for you. For difference between Service and IntentService follow this link.

public class service extends Service implements SensorEventListener {
@Override

public void onCreate() {
    super.onCreate();
//register your sensor manager listener here
}

@Override
public void onDestroy() {
//unregister your listener here
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        //detect the shake and do your work here
    }

}

这篇关于我的IntentService在后台如何始终收听设备的震动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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