我是Android新手。我正在开发一个记录传感器数据的应用程序。我有两个按钮的活动。当用户按下开始按钮时,我想要启动服务并记录传感器数据,直到用户按下停止按钮。我不确定我想要使用哪种类型的服务。我读了关于本地和远程服务的内容,但我实际上并没有明白其中的差别。Android服务在应用程序死亡时重新启动
我试过至今:
我创建了一个活动与启动和停止本地服务两个按钮:
//Start Service
startService(new Intent(this, MyService.class));
//Stop Service
stopService(new Intent(this, MyService.class));
我还创建了一个棘手的服务,onStartCommand()
开始记录传感器数据:
public int onStartCommand(Intent intent, int flags, int startId) {
mSensorManager.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, magnetometer,
SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, lightSensor,
SensorManager.SENSOR_DELAY_UI);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return Service.START_STICKY;
}
AndroidManifest.xml:
<service android:name=".MyService" android:enabled="true" android:process=":HelloSensors_background"/>
它运行良好,但问题是当我启动该服务的kill the application时,此服务重新启动并丢失所有以前记录的数据。为了使服务能够顺利运行并且即使在应用程序被杀害时仍继续运行,以避免丢失任何已记录的数据,我该如何使用?
你是什么意思杀死应用程序?如果用户转到应用程序的设置页面并选择“强制停止”,服务将被停止,但对此无能为力。 – Premsuraj 2013-03-18 10:03:34
[This](http://www.youtube.com/watch?v=XVyXCrM-3zw)就是我的意思,通过杀死应用程序 – user1135357 2013-03-18 10:11:29
我第二个问题_你是什么意思“杀死application_”?但是,我想指出的是,服务_can_可以与应用程序分开,以便如果用户停止_app_,_service_可以继续运行。 – 2013-03-18 10:11:58