0

I want to disable tasks for 1 hour with countdowntimer. but when app is close, countdowntimer is stop working

I have tried Intent service but seems like its not working

final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Context mContext;

public IBinder onBind(Intent intent) {
    return null;
}

public void onCreate() {
    super.onCreate();
    mContext = this;
    startService();
}

private void startService() {
    scheduler.scheduleAtFixedRate(runner, 0, 30, TimeUnit.SECONDS);
}

final Runnable runner = new Runnable() {
    public void run() {
        mHandler.sendEmptyMessage(1212);
        Toast.makeText(mContext, "TImer is out", Toast.LENGTH_SHORT).show();
    }
};

public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
}

private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        //do what ever you want as 3hrs is completed
    }
};
eibersji
  • 1,218
  • 4
  • 29
  • 52
Manish Stark
  • 110
  • 6
  • tried saving DateTime.Now on AppClosing and start new Counter on AppStart with adding the passed time since closed to the Counter? – Aiko West Sep 23 '19 at 05:34

2 Answers2

1

You can use AlarmManager for performing tasks outside of your app's lifetime. When setting the time for the alarm, simply calculate the time after 1 hour and set alarm for that time. You can check the docs in here: https://developer.android.com/training/scheduling/alarms

Aram Sheroyan
  • 588
  • 1
  • 6
  • 17
  • These days you should probably use JobScheduler rather than AlarmManager directly. – Gabe Sechan Sep 23 '19 at 05:59
  • @GabeSechan If I am not mistaken, JobSceduler requires a min SDK 21 and it is better to use `WorkManager` instead. It would be a better option if the task is not required to be launched at a specific time (such as network requests) since it is designed to respect the idle state – Aram Sheroyan Sep 23 '19 at 06:12
  • WorkManager has its use cases, but its not a general replacement for JobScheduler/AlarmManager, especially not for a 1 off timer. JobScheduler has been backported via support libraries before v21, although really you probably shouldn't be writing apps that go further back than that without very good reason, the cost/user isn't there. – Gabe Sechan Sep 23 '19 at 13:41
0

You can use JobScheduler like this:

private static final int JOB_ID=1;

JobScheduler mJobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, new ComponentName(getPackageName(),SchedulerService.class.getName()));
builder.setMinimumLatency(60 * 60 * 1000);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
mJobScheduler.schedule(builder.build());

SchedulerService is your scheduling service

public class SchedulerService extends JobService {
private static final String TAG = "SchedulerService";
@Override
public boolean onStartJob(JobParameters params) {
    // resume your work
    return false;
}

@Override
public boolean onStopJob(JobParameters params) {
    return false;
}}
Ankit
  • 1,068
  • 6
  • 10