0

I'm trying to create a timer that would launch in the background once onDestroy() is triggered, and once the timer reaches 6 hours, SharedPreferences will be used to make a change in the app.

I was wondering... what would be the correct way to setup CountDownTimer or anything similar it for onDestroy().

I will answer anything if needed. Thank you.

Meowminator
  • 61
  • 1
  • 7
  • 1
    Have you tried using a Service? – TheWanderer Sep 11 '18 at 00:09
  • It's not about using a Service and more about what the code to establish the countdown would look like in this scenario. – Meowminator Sep 11 '18 at 04:43
  • it's all about a Service. It's the only way to do what you want. – TheWanderer Sep 11 '18 at 09:35
  • I searched for questions similar to mine on how to create a countdown Service, but it doesn't really explain how to launch it only when the app is closed. https://stackoverflow.com/questions/22496863/how-to-run-countdowntimer-in-a-service-in-android – Meowminator Sep 12 '18 at 00:13
  • Use `startForegroundService()` in your Activity's `onDestroy()` method. – TheWanderer Sep 12 '18 at 00:15
  • Looks like the minimum required sdk to use `startForegroundService()` is 26. Only very little amount of phones will be able to use the app in this case. Not a lot of phones are 26. I'll just skip on the countdown thing thanks. – Meowminator Sep 12 '18 at 22:31
  • 1
    You can use `ContextCompat.startForegroundService(context, serviceIntent)`, which will start it as a foreground service on API 26+ and a normal service on API 25-. – TheWanderer Sep 12 '18 at 22:44
  • Yay! I got it working finally. Thank you so much. I will add the answer in about an hour. – Meowminator Sep 13 '18 at 00:14
  • Apparently `startForegroundService` crashes if the app is relaunched after 5 seconds. Seems like no way to get past that. Only for API 26 and above. – Meowminator Sep 16 '18 at 00:24
  • you need to call startForeground inside the Service's onCreate method. – TheWanderer Sep 16 '18 at 00:26
  • But I do agree with you. Google forced this behavior on Oreo without properly testing it. You might run into crashes on some devices, since the system won't always start the Service within the 5 second limit. – TheWanderer Sep 16 '18 at 00:28
  • Awesome thanks. Now the service works without crashing. Going to make sure that everything works. – Meowminator Sep 16 '18 at 02:49
  • @TheWanderer I've written an answer. SharedPreferences works as intended after an one hour timer, and without the `startForegroundService` crashing. Thanks for all your help. – Meowminator Sep 16 '18 at 21:28

1 Answers1

1

After about a week, a working answer came up. This answer includes what's related to how to make the service run. In this scenario, I'm building a CountDownTimer service.

AndroidManifest.xml

<service android:name=".TimeService"/>

MainActivity.java

import android.content.Intent;
import android.support.v4.content.ContextCompat;

public class MainActivity extends AppCompatActivity {
       Intent i;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           i = new Intent(this, TimeService.class);

           stopService(i); //To stop the service the next time the app is launched.
       }

       @Override
       protected void onDestroy() {
           launchService(); //Launches the service once when app shuts down.
           super.onDestroy();
       }

       public void launchService() { //How to launch the service, depending the phone's API.
            if(Build.VERSION.SDK_INT >= 26) {
                 startForegroundService(new Intent(this, TimeService.class));
            }
            else{
                Intent i;
                i = new Intent(this, TimeService.class);
                ContextCompat.startForegroundService(this, i);
            }
       }
}

TimeService.java

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.support.annotation.Nullable;

public class TimeService extends Service {

   CountDownTimer cdt = null;
   private SharedPreferences pref;
   //Things you want SharedPreferences to change.
   Intent i;

   @Override
   public void onCreate() {
       i = new Intent(this, TimeService.class);
       startService(i);
       pref = this.getSharedPreferences("myAppPref", MODE_PRIVATE);
       cdt = new CountDownTimer(3600000, 1000) { //One hour timer with one second interval.

           @Override
           public void onTick(long millisUntilFinished) {

           }

           @Override
           public void onFinish() {
                //Whatever you need SharedPreferences to change here.
           }
       };
       cdt.start();
   }

   @Nullable
   @Override
   public IBinder onBind(Intent intent) {
       return null;
   }
}
Meowminator
  • 61
  • 1
  • 7