Java 使用非服务对象的Android后台应用程序
我正在尝试制作一个用于收集传感器数据的后台应用程序。我的传感器管理器在IntentService中。但是从这个服务中,我调用了其他非服务包中的方法。问题是,当我退出活动时,我的服务被破坏 有什么解决办法吗。我可以使用非服务对象吗 这是我的服务代码Java 使用非服务对象的Android后台应用程序,java,android,android-intent,background,Java,Android,Android Intent,Background,我正在尝试制作一个用于收集传感器数据的后台应用程序。我的传感器管理器在IntentService中。但是从这个服务中,我调用了其他非服务包中的方法。问题是,当我退出活动时,我的服务被破坏 有什么解决办法吗。我可以使用非服务对象吗 这是我的服务代码 package tinygsn.services; import java.io.Serializable; import tinygsn.beans.InputStream; import tinygsn.beans.Stream
package tinygsn.services;
import java.io.Serializable;
import tinygsn.beans.InputStream;
import tinygsn.beans.StreamElement;
import tinygsn.beans.StreamSource;
import tinygsn.beans.VSensorConfig;
import tinygsn.controller.AbstractController;
import tinygsn.controller.AndroidControllerListVSNew;
import tinygsn.model.wrappers.AbstractWrapper;
import tinygsn.model.wrappers.AndroidGyroscopeWrapper;
import android.app.Activity;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
public class TestVS extends IntentService implements SensorEventListener{
private VSensorConfig config = null;
private static final String TAG = "ServiceVS";
public AndroidControllerListVSNew VSNewController;
public AbstractWrapper w;
private SensorManager mSensorManager;
private Sensor mSensor;
public TestVS()
{
super("service");
}
public TestVS(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent)
{
// Log.v(TAG, "Starts Service: " + config.toString());
Bundle b = intent.getExtras();
config = (VSensorConfig) b.get("config");
Log.i("Service", config.getInputStreams().toString());
for (InputStream inputStream : config.getInputStreams()) {
for (StreamSource streamSource : inputStream.getSources()) {
w = streamSource.getWrapper();
Log.v(TAG, w.toString());
Activity activity = config.getController().getActivity();
mSensorManager = (SensorManager) activity
.getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
mSensorManager.registerListener(this, mSensor,
SensorManager.SENSOR_DELAY_NORMAL);
while (w.isActive()) {
try {
Thread.sleep(w.getSamplingRate());
((AndroidGyroscopeWrapper) w).getLastKnownData();
Log.i("data read ", "data read");
}
catch (InterruptedException e) {
Log.e(e.getMessage(), e.toString());
}
}
}
}
}
@Override
public void onSensorChanged(SensorEvent event) {
double x = event.values[0];
double y = event.values[1];
double z = event.values[2];
StreamElement streamElement = new StreamElement(w.getFieldList(), w.getFieldType(),
new Serializable[] { x, y, z });
((AndroidGyroscopeWrapper) w).theLastStreamElement = streamElement;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
谢谢你能给我看一些相关的代码和详细说明吗?像这样,我不知道您的确切意思或试图做什么。“问题是,当我退出活动时,我的服务被破坏”--不,它在
onHandleIntent()
结束时被破坏IntentService
不是为您的场景设计的。我建议您使用常规服务,其中您使用自己的后台线程来处理传感器数据。我添加了我的服务包装包不是服务的代码,并且代码中有一段时间它没有变为false,因此不应销毁它