2

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?

Lucifer
  • 29,392
  • 25
  • 90
  • 143
venni
  • 594
  • 1
  • 4
  • 19

2 Answers2

2

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

Community
  • 1
  • 1
pandavid87
  • 151
  • 2
  • Hmm, I thought with the declaration of "singleTOP" in the Manifest for the view it would be no problem to get the activity. Nevertheless I have set "toLaunch .setAction("android.intent.action.MAIN");" & "toLaunch .addCategory("android.intent.category.LAUNCHER");" to my notification with no success. – venni Feb 19 '12 at 12:50
  • What I also figured out is that also the GPS-Listener from my GPS-Helper class stopped working, cause the GPS-Signal in the notification bar was switched off. I do not know if this is cause of shutting down my activity from the OS or shutting down the GPS-Listener class. If my activity would stay in background it will call the GPS-Helper class every second. This is driving me crazy. May be I am completely on the wrong way?... – venni Feb 19 '12 at 12:53
  • It sounds like you are doing everything from an Activity. I think a Service would be the better way to go for doing this constant updating to a notification. I'm not sure if you can dynamically put extras to an intent to preserve your data and extract it later in the activity though. – pandavid87 Feb 19 '12 at 20:56
  • I also think that a service would be the right way. So I have to read something about creating/using services... – venni Feb 19 '12 at 21:34
0

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

havchr
  • 1,132
  • 1
  • 10
  • 25