Android 中的后台数据收集器在 App 终止时重新启动
Background data collector in Android restarts when App is terminated
我想用我的应用程序收集加速度计数据。因此我创建了一个服务,所以应用程序在后台收集数据,即使应用程序不再打开。
问题是,当我关闭应用程序时,服务也会终止并重新启动。在此期间它不会收集数据。
我试了START_REDELIVER_INTENT
和START_STICKY
,结果都是一样的:当应用关闭时,服务会重新启动。
有什么方法可以防止应用程序终止时服务完全停止?
Note that my service is running in a separate process.
这是我的代码:
@Override
public void onCreate() {
// start thread
HandlerThread thread = new HandlerThread("SensorData", Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
looper = thread.getLooper();
handler = new BGHndlr(looper);
sensorMngr = (SensorManager) getSystemService(SENSOR_SERVICE);
reader = new SensorReader(sensorMngr);
accSensor = reader.getSingleSensorOfType(Sensor.TYPE_ACCELEROMETER);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Message message = handler.obtainMessage();
message.arg1 = startId;
handler.sendMessage(message);
return START_REDELIVER_INTENT;
}
嵌套class:
public class BGHndlr extends Handler implements SensorEventListener {
public BGHndlr(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
oSensorManager.registerListener(this, oAcceleroMeter, SensorManager.SENSOR_DELAY_FASTEST);
}
//int i = 0;
@Override
public void onSensorChanged(SensorEvent event) {
// do something
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {}
}
both with the same result: when the app closes, the service restarts.
这是一个 ,Android
应用程序开发人员对此无能为力。
Note that my service is running in a separate process.
和以前一样(你的问题都证明了这一点)...也就是说,
的答案
Is there way to prevent the service completely from stopping when the app is terminated?
不是。你和其他人一样,应该坚持 Android
组件的生命周期。
我想用我的应用程序收集加速度计数据。因此我创建了一个服务,所以应用程序在后台收集数据,即使应用程序不再打开。
问题是,当我关闭应用程序时,服务也会终止并重新启动。在此期间它不会收集数据。
我试了START_REDELIVER_INTENT
和START_STICKY
,结果都是一样的:当应用关闭时,服务会重新启动。
有什么方法可以防止应用程序终止时服务完全停止?
Note that my service is running in a separate process.
这是我的代码:
@Override
public void onCreate() {
// start thread
HandlerThread thread = new HandlerThread("SensorData", Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
looper = thread.getLooper();
handler = new BGHndlr(looper);
sensorMngr = (SensorManager) getSystemService(SENSOR_SERVICE);
reader = new SensorReader(sensorMngr);
accSensor = reader.getSingleSensorOfType(Sensor.TYPE_ACCELEROMETER);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Message message = handler.obtainMessage();
message.arg1 = startId;
handler.sendMessage(message);
return START_REDELIVER_INTENT;
}
嵌套class:
public class BGHndlr extends Handler implements SensorEventListener {
public BGHndlr(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
oSensorManager.registerListener(this, oAcceleroMeter, SensorManager.SENSOR_DELAY_FASTEST);
}
//int i = 0;
@Override
public void onSensorChanged(SensorEvent event) {
// do something
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {}
}
both with the same result: when the app closes, the service restarts.
这是一个 Android
应用程序开发人员对此无能为力。
Note that my service is running in a separate process.
和以前一样(你的问题都证明了这一点)...也就是说,
的答案Is there way to prevent the service completely from stopping when the app is terminated?
不是。你和其他人一样,应该坚持 Android
组件的生命周期。