David Small
David Small

Reputation: 581

How to Prevent Android From Killing My Activity. Services Maybe?

I'm pretty new to Android development. I have an app that records how long the user is at work and shows them how much they've earned. It works perfectly, until Android kills the activity. Is there a way to prevent this? I've read a little about service, but I'm unsure if they are what I need or even how to use them. Any help would be awesome. Thank you for your time. (edit) This is my clockIn() method. It's called when the user presses the clock in button.

public void clockIn(){
    start = System.currentTimeMillis();
    mTimer = new Timer();
    mTimer.scheduleAtFixedRate(new TimerTask() {


        @Override
        public void run() {
            now = System.currentTimeMillis();
            seconds = date.getSeconds();   
            elapsedTime = now - start;
            earnings = (wageHour/60/60/1000)*elapsedTime;
            runOnUiThread(new Runnable() {
                public void run() {

                    timeDisplay.setText("$" + String.format("%.4f", earnings));


               }
           });

             }
        }, 0, 100);
}

Upvotes: 0

Views: 229

Answers (2)

mach
mach

Reputation: 8395

You may want to check out foreground notifications for a Service.

Upvotes: 1

electrofant
electrofant

Reputation: 945

How are you tracking time? The easiest way to keep track of time is to save timestamps in a database table. e.g. checkin/checkout times. There should be no problem with killed activities.

Upvotes: 2

Related Questions