Question

I am currently working on a GPS tracking App (only the distance, not storing each value or displaying in a MapView) for a car-drivers logbook. Cause of a docked phone, I do not care about power consumption. My implementation so far is an activity that calls a GPS-Helper class which is getting the long/lat. The activity itself calculates the driven distance, displays it for the user and starts a notification bar that also displays the distance. To keep the activity active and not killed by the OS, I am using a PARTIAL WakeLock for the activity.

My problem is that this is not working correctly, cause my App seems to be killed by the OS inspite of the WakeLock. I think that it is killed, cause when I click on the notification bar item (after 15-30 min. for example) to see the driven distance in my running activity, the activity is shown as it is to start a new GPS-track instead of displaying the driven distance from the former started track. The WAKELOCK Permission is correctly set in the Manifest.

My question now is to get know if this costruct could be working or is there a better way to do this?

Was it helpful?

Solution

Your problem may be with the Intent you are launching when you click on the notification. This intent is most likely thinking that you want to launch a brand new Activity rather than returning the old activity to the foreground.

This link may help you to do what you want:

How to bring Android existing activity to front via notification

OTHER TIPS

You should use a service which calls startForground, which requires a notification. This notification will be your entry point back into the app. The service can run in the background and log coordinates without depending on the life cycle of your Activity.

    @Override
        public int onStartCommand(Intent intent, int flags, int startId) {

            if(intent.getAction().equals(DRIVELOG_ACTION_STOPLOGGING)){               handleStopLoggingCommand(intent.getBooleanExtra(SAVE_LOG,false));
            }
            else if(intent.getAction().equals(DRIVELOG_ACTION_STARTLOGGING)){
                handleStartLoggingCommand();
            }
            return START_STICKY;
        }


   private void handleStartLoggingCommand() {
        startForeground(DriveLoggerNotification.notificationId,DriveLoggerNotification.createInDriveLogNotification(this,driveLogLiveData));
    if(googleApiClient.isConnected()){
        startMonitoringLocation();
    }else {
        googleApiClient.connect();
    }

}

This code is from my GpsLoggingService

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top