Search code examples
javaandroidandroid-studiosharedpreferencescountdowntimer

What would be the correct way to set up a CountDownTimer to run onDestroy()?


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.


Solution

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