Best way to make an action x times without using a Handler?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I want to replace this function:



private void transmit(){
final Handler mHandler = new Handler();

Toast.makeText(this, "Wait for the connection to stablish",Toast.LENGTH_LONG).show();

mHandler.postDelayed(new Runnable() {
@Override
public void run() {

//Log.d("BtSending", "run: Sending..");


mBluetoothLeService.writeCharacteristic(superString,bluetoothGattCharacteristicHM_10);

mHandler.postDelayed(this,250);

}
},1000);

}


for something more pretty, it works just fine but a friend told me that there was a more efficient way to do it, but i dont remember what he told me. Help me please, Thanks in advance.










share|improve this question































    0















    I want to replace this function:



    private void transmit(){
    final Handler mHandler = new Handler();

    Toast.makeText(this, "Wait for the connection to stablish",Toast.LENGTH_LONG).show();

    mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {

    //Log.d("BtSending", "run: Sending..");


    mBluetoothLeService.writeCharacteristic(superString,bluetoothGattCharacteristicHM_10);

    mHandler.postDelayed(this,250);

    }
    },1000);

    }


    for something more pretty, it works just fine but a friend told me that there was a more efficient way to do it, but i dont remember what he told me. Help me please, Thanks in advance.










    share|improve this question



























      0












      0








      0








      I want to replace this function:



      private void transmit(){
      final Handler mHandler = new Handler();

      Toast.makeText(this, "Wait for the connection to stablish",Toast.LENGTH_LONG).show();

      mHandler.postDelayed(new Runnable() {
      @Override
      public void run() {

      //Log.d("BtSending", "run: Sending..");


      mBluetoothLeService.writeCharacteristic(superString,bluetoothGattCharacteristicHM_10);

      mHandler.postDelayed(this,250);

      }
      },1000);

      }


      for something more pretty, it works just fine but a friend told me that there was a more efficient way to do it, but i dont remember what he told me. Help me please, Thanks in advance.










      share|improve this question
















      I want to replace this function:



      private void transmit(){
      final Handler mHandler = new Handler();

      Toast.makeText(this, "Wait for the connection to stablish",Toast.LENGTH_LONG).show();

      mHandler.postDelayed(new Runnable() {
      @Override
      public void run() {

      //Log.d("BtSending", "run: Sending..");


      mBluetoothLeService.writeCharacteristic(superString,bluetoothGattCharacteristicHM_10);

      mHandler.postDelayed(this,250);

      }
      },1000);

      }


      for something more pretty, it works just fine but a friend told me that there was a more efficient way to do it, but i dont remember what he told me. Help me please, Thanks in advance.







      android






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 5:09









      eyllanesc

      89.7k113565




      89.7k113565










      asked Nov 25 '18 at 5:06









      Kevin Anthony Zhang PlazaKevin Anthony Zhang Plaza

      31




      31
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Handlers are the recommended way to run recurring actions in quick succession. That doesn't mean you can't make the code a bit neater, though.



          private final int MSG_TRANSMIT = 100;

          private TransmitHandler handler = new TransmitHandler();

          private void transmit() {
          //...

          handler.sendInitialTransmit();
          }

          public class TransmitHandler extends Handler {
          public TransmitHandler() {
          super(Looper.getMainLooper());
          }

          @Override
          public void handleMessage(Message msg) {
          switch (msg.what) {
          case MSG_TRANSMIT:
          mBluetoothLeService.writeCharacteristic(superString,bluetoothGattCharacteristicHM_10);
          if (shouldTransmit) sendTransmit(); //shouldTransmit is an arbitrary boolean so you can stop the loop when needed
          break;
          }
          }

          public void sendInitialTransmit() {
          sendEmptyMessageAtTime(MSG_TRANSMIT, SystemClock.uptimeMillis() + 1000);
          }

          public void sendTransmit() {
          sendEmptyMessageAtTime(MSG_TRANSMIT, SystemClock.uptimeMillis() + 250); //where 250 is your delay
          }

          public void cancelTransmit() {
          removeMessages(MSG_TRANSMIT); //if MSG_TRANSMIT is currently queued to be executed, calling cancelTrasmit() will remove it from the queue
          }
          }





          share|improve this answer
























          • Do you think it could be better using a service instead?

            – Kevin Anthony Zhang Plaza
            Nov 25 '18 at 17:50











          • How would you use a Service? Services are just components that can run constantly in the background. You'd still need to put something in the Service to run an action every x seconds.

            – TheWanderer
            Nov 25 '18 at 17:51











          • Sorry I think I miss to mention a data, I need to send it every x second infinite times. But this actually answered my question haha thanks

            – Kevin Anthony Zhang Plaza
            Nov 26 '18 at 3:53














          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464814%2fbest-way-to-make-an-action-x-times-without-using-a-handler%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Handlers are the recommended way to run recurring actions in quick succession. That doesn't mean you can't make the code a bit neater, though.



          private final int MSG_TRANSMIT = 100;

          private TransmitHandler handler = new TransmitHandler();

          private void transmit() {
          //...

          handler.sendInitialTransmit();
          }

          public class TransmitHandler extends Handler {
          public TransmitHandler() {
          super(Looper.getMainLooper());
          }

          @Override
          public void handleMessage(Message msg) {
          switch (msg.what) {
          case MSG_TRANSMIT:
          mBluetoothLeService.writeCharacteristic(superString,bluetoothGattCharacteristicHM_10);
          if (shouldTransmit) sendTransmit(); //shouldTransmit is an arbitrary boolean so you can stop the loop when needed
          break;
          }
          }

          public void sendInitialTransmit() {
          sendEmptyMessageAtTime(MSG_TRANSMIT, SystemClock.uptimeMillis() + 1000);
          }

          public void sendTransmit() {
          sendEmptyMessageAtTime(MSG_TRANSMIT, SystemClock.uptimeMillis() + 250); //where 250 is your delay
          }

          public void cancelTransmit() {
          removeMessages(MSG_TRANSMIT); //if MSG_TRANSMIT is currently queued to be executed, calling cancelTrasmit() will remove it from the queue
          }
          }





          share|improve this answer
























          • Do you think it could be better using a service instead?

            – Kevin Anthony Zhang Plaza
            Nov 25 '18 at 17:50











          • How would you use a Service? Services are just components that can run constantly in the background. You'd still need to put something in the Service to run an action every x seconds.

            – TheWanderer
            Nov 25 '18 at 17:51











          • Sorry I think I miss to mention a data, I need to send it every x second infinite times. But this actually answered my question haha thanks

            – Kevin Anthony Zhang Plaza
            Nov 26 '18 at 3:53


















          0














          Handlers are the recommended way to run recurring actions in quick succession. That doesn't mean you can't make the code a bit neater, though.



          private final int MSG_TRANSMIT = 100;

          private TransmitHandler handler = new TransmitHandler();

          private void transmit() {
          //...

          handler.sendInitialTransmit();
          }

          public class TransmitHandler extends Handler {
          public TransmitHandler() {
          super(Looper.getMainLooper());
          }

          @Override
          public void handleMessage(Message msg) {
          switch (msg.what) {
          case MSG_TRANSMIT:
          mBluetoothLeService.writeCharacteristic(superString,bluetoothGattCharacteristicHM_10);
          if (shouldTransmit) sendTransmit(); //shouldTransmit is an arbitrary boolean so you can stop the loop when needed
          break;
          }
          }

          public void sendInitialTransmit() {
          sendEmptyMessageAtTime(MSG_TRANSMIT, SystemClock.uptimeMillis() + 1000);
          }

          public void sendTransmit() {
          sendEmptyMessageAtTime(MSG_TRANSMIT, SystemClock.uptimeMillis() + 250); //where 250 is your delay
          }

          public void cancelTransmit() {
          removeMessages(MSG_TRANSMIT); //if MSG_TRANSMIT is currently queued to be executed, calling cancelTrasmit() will remove it from the queue
          }
          }





          share|improve this answer
























          • Do you think it could be better using a service instead?

            – Kevin Anthony Zhang Plaza
            Nov 25 '18 at 17:50











          • How would you use a Service? Services are just components that can run constantly in the background. You'd still need to put something in the Service to run an action every x seconds.

            – TheWanderer
            Nov 25 '18 at 17:51











          • Sorry I think I miss to mention a data, I need to send it every x second infinite times. But this actually answered my question haha thanks

            – Kevin Anthony Zhang Plaza
            Nov 26 '18 at 3:53
















          0












          0








          0







          Handlers are the recommended way to run recurring actions in quick succession. That doesn't mean you can't make the code a bit neater, though.



          private final int MSG_TRANSMIT = 100;

          private TransmitHandler handler = new TransmitHandler();

          private void transmit() {
          //...

          handler.sendInitialTransmit();
          }

          public class TransmitHandler extends Handler {
          public TransmitHandler() {
          super(Looper.getMainLooper());
          }

          @Override
          public void handleMessage(Message msg) {
          switch (msg.what) {
          case MSG_TRANSMIT:
          mBluetoothLeService.writeCharacteristic(superString,bluetoothGattCharacteristicHM_10);
          if (shouldTransmit) sendTransmit(); //shouldTransmit is an arbitrary boolean so you can stop the loop when needed
          break;
          }
          }

          public void sendInitialTransmit() {
          sendEmptyMessageAtTime(MSG_TRANSMIT, SystemClock.uptimeMillis() + 1000);
          }

          public void sendTransmit() {
          sendEmptyMessageAtTime(MSG_TRANSMIT, SystemClock.uptimeMillis() + 250); //where 250 is your delay
          }

          public void cancelTransmit() {
          removeMessages(MSG_TRANSMIT); //if MSG_TRANSMIT is currently queued to be executed, calling cancelTrasmit() will remove it from the queue
          }
          }





          share|improve this answer













          Handlers are the recommended way to run recurring actions in quick succession. That doesn't mean you can't make the code a bit neater, though.



          private final int MSG_TRANSMIT = 100;

          private TransmitHandler handler = new TransmitHandler();

          private void transmit() {
          //...

          handler.sendInitialTransmit();
          }

          public class TransmitHandler extends Handler {
          public TransmitHandler() {
          super(Looper.getMainLooper());
          }

          @Override
          public void handleMessage(Message msg) {
          switch (msg.what) {
          case MSG_TRANSMIT:
          mBluetoothLeService.writeCharacteristic(superString,bluetoothGattCharacteristicHM_10);
          if (shouldTransmit) sendTransmit(); //shouldTransmit is an arbitrary boolean so you can stop the loop when needed
          break;
          }
          }

          public void sendInitialTransmit() {
          sendEmptyMessageAtTime(MSG_TRANSMIT, SystemClock.uptimeMillis() + 1000);
          }

          public void sendTransmit() {
          sendEmptyMessageAtTime(MSG_TRANSMIT, SystemClock.uptimeMillis() + 250); //where 250 is your delay
          }

          public void cancelTransmit() {
          removeMessages(MSG_TRANSMIT); //if MSG_TRANSMIT is currently queued to be executed, calling cancelTrasmit() will remove it from the queue
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 5:20









          TheWandererTheWanderer

          7,95431230




          7,95431230













          • Do you think it could be better using a service instead?

            – Kevin Anthony Zhang Plaza
            Nov 25 '18 at 17:50











          • How would you use a Service? Services are just components that can run constantly in the background. You'd still need to put something in the Service to run an action every x seconds.

            – TheWanderer
            Nov 25 '18 at 17:51











          • Sorry I think I miss to mention a data, I need to send it every x second infinite times. But this actually answered my question haha thanks

            – Kevin Anthony Zhang Plaza
            Nov 26 '18 at 3:53





















          • Do you think it could be better using a service instead?

            – Kevin Anthony Zhang Plaza
            Nov 25 '18 at 17:50











          • How would you use a Service? Services are just components that can run constantly in the background. You'd still need to put something in the Service to run an action every x seconds.

            – TheWanderer
            Nov 25 '18 at 17:51











          • Sorry I think I miss to mention a data, I need to send it every x second infinite times. But this actually answered my question haha thanks

            – Kevin Anthony Zhang Plaza
            Nov 26 '18 at 3:53



















          Do you think it could be better using a service instead?

          – Kevin Anthony Zhang Plaza
          Nov 25 '18 at 17:50





          Do you think it could be better using a service instead?

          – Kevin Anthony Zhang Plaza
          Nov 25 '18 at 17:50













          How would you use a Service? Services are just components that can run constantly in the background. You'd still need to put something in the Service to run an action every x seconds.

          – TheWanderer
          Nov 25 '18 at 17:51





          How would you use a Service? Services are just components that can run constantly in the background. You'd still need to put something in the Service to run an action every x seconds.

          – TheWanderer
          Nov 25 '18 at 17:51













          Sorry I think I miss to mention a data, I need to send it every x second infinite times. But this actually answered my question haha thanks

          – Kevin Anthony Zhang Plaza
          Nov 26 '18 at 3:53







          Sorry I think I miss to mention a data, I need to send it every x second infinite times. But this actually answered my question haha thanks

          – Kevin Anthony Zhang Plaza
          Nov 26 '18 at 3:53






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464814%2fbest-way-to-make-an-action-x-times-without-using-a-handler%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Academy of Television Arts & Sciences

          L'Équipe

          1995 France bombings