How to change the speed or period of a recurring task using ScheduledExecutorService?

I have a modified version of the application for the bluetooth chat example. I installed a ScheduledExecutorService that sends a command via bluetooth at a predetermined speed using scheduleAtFixedRate .

I set PreferenceActivity so the user can change the period. But I'm not sure how to get current recurring tasks with an updated period. Do I need to somehow cancel and restart the ScheduledExecutorService ?

Here are the relevant parts of my code.

 private ScheduledExecutorService scheduleTaskExecutor; public long ReadInterval = 1; ... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); scheduleTaskExecutor = Executors.newScheduledThreadPool(5); ... // This schedule a task to run every 1 second: scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() { public void run() { // If you need update UI, simply do this: runOnUiThread(new Runnable() { public void run() { // update your UI component here. if (connected == true) { sendMessage("READ"); if (D) Log.i(TAG, "In Run!"); } } }); } }, 0, ReadInterval, TimeUnit.SECONDS); } 

And I tried to update ReadInterval here. ReadInterval updated, but the recurring period of the command is not updated.

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (D) Log.d(TAG, "onActivityResult " + resultCode); switch (requestCode) { case REQUEST_CONNECT_DEVICE: ... case REQUEST_ENABLE_BT: ... case REQUEST_SETTINGS: // When returning from settings activity SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); String Pref = sharedPref.getString(SettingsActivity.KEY_PREF_READINTERVAL, ""); ReadInterval = Long.valueOf(Pref); Toast.makeText(this, Pref, Toast.LENGTH_SHORT).show(); Log.d(TAG, "Settings Activity Result"); } } 
+5
source share
1 answer

I improved the answer a bit (mainly because I also had to use the ScheduledExecutorService ). Please use this code instead of the previous one that I posted, because the previous one was really for performance, with no good reason.

 private ScheduledExecutorService scheduledExecutorService; private ScheduledFuture<?> futureTask; private Runnable myTask; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Your executor, you should instanciate it once for all scheduledExecutorService = Executors.newScheduledThreadPool(5); // Since your task won't change during runtime, you can instanciate it too once for all myTask = new Runnable() { @Override public void run() { // Your code to run periodically } }; } /** * This method will reschedule "myTask" with the new param time */ public void changeReadInterval(long time) { if(time > 0) { if (futureTask != null) { futureTask.cancel(true); } futureTask = scheduledExecutorService.scheduleAtFixedRate(myTask, 0, time, TimeUnit.SECONDS); } } 

Now, to transfer your task at run time, use the changeReadInterval(time); method changeReadInterval(time);

It will undo the previous β€œtimer” if it has been set, and will transfer the new one.

+10
source

Source: https://habr.com/ru/post/1213774/


All Articles