Android foreground service lose context

北城以北 提交于 2019-12-12 03:45:33

问题


I have a little problem understanding what's going on with a service lifecycle that I created. My Application declare and use a service "LocalisationSevice" which its goal is to start a location listener and add the updated location to an array.

Simple as that. The service run foreground.

The problem is that if I manually kill my application though the task switcher or eclipse the service will stay (GPS and notification are here in the notification area). Thats fine, it is what I want, if I click on the notification it will take me back to my application saved state. But now the problem is if I launch the application by clicking on the icon instead of the service notification it will start the application from the start. But it should restore the application into the latest state no ? Like if I have clicked on the service notification.

I start the service like this:

Intent i = new Intent(this, LocalisationService.class);
startService(i);

Here is the code of the service:

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);
}

If I kill the application and then relaunch it by the service it will call onStartCommand(); again. Also it seems that I lost the link with the location listener, when i'll destroy the service after relaunching it, the notification icon disapear but not the GPS icon.

Edit: Here is the interesting part of my Manifest file The part about the actvity from where the service is started

    <activity
        android:name="com.wayzup.activity.RouteActivity"
        android:screenOrientation="portrait"
        android:launchMode="singleTop" >
    </activity>

And the part about my service

  <service android:name="com.wayzup.services.LocalisationService" >
        </service>

来源:https://stackoverflow.com/questions/12564620/android-foreground-service-lose-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!