我有一个小问题,了解正在发生的事情与我创建了一个服务生命周期。 我的应用程序声明和使用服务“LocalisationSevice”,它的目标是启动一个位置监听器,并添加更新的位置到一个数组。
就那么简单。 该服务运行前景。
问题是,如果我手动杀了我的申请,虽然任务切换或者Eclipse的服务将保持(GPS和通知在此通知区域)。 多数民众赞成罚款,这是我想要的东西,如果我点击该通知,它会带我回保存的状态我的应用程序。 但现在的问题是,如果我通过点击图标,而不是服务通知启动该应用程序将开始从一开始应用。 但是,应该恢复应用到最新的状态,不是吗? 如果我点击了该服务的通知等。
我开始喜欢这个服务:
Intent i = new Intent(this, LocalisationService.class);
startService(i);
这里是服务的代码:
public class LocalisationService extends Service {
private LocationManager locationManager;
private LocationListener locationListener;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification = new Notification(R.drawable.iconsmall,
"Notification text", System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT;
Intent i = new Intent(this, RouteActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
notification.setLatestEventInfo(this, "Notification title",
"notification message)", pi);
startForeground(1337, notification);
startListenLocation();
return (START_NOT_STICKY);
}
@Override
public void onDestroy() {
stopListenLocation();
stopForeground(true);
}
public void startListenLocation() {
locationManager = (LocationManager) getApplicationContext()
.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
CurrentRouteManager.getSharedManager().updateWithLocation(
location);
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
200, locationListener);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 200, locationListener);
}
public void stopListenLocation() {
locationManager.removeUpdates(locationListener);
}
如果我杀的应用程序,然后由服务重新启动它,它会调用onStartCommand(); 再次。 此外,它似乎我失去了位置监听器的链接,当我重新发动会它,通知图标disapear而不是GPS图标后导致的服务。
编辑:这是我的清单文件的有趣的部分大约从服务启动,其中actvity的部分
<activity
android:name="com.wayzup.activity.RouteActivity"
android:screenOrientation="portrait"
android:launchMode="singleTop" >
</activity>
而关于我的服务的一部分
<service android:name="com.wayzup.services.LocalisationService" >
</service>