My program initializes the bluetooth device at the touch of a button. At the same time, the animation of this button should start - rotate

@Override public void onClick(View v) { switch (v.getId()) { case R.id.toggleGreenLed: try { int value = 0; value = (greenButton.isChecked() ? 1 : 0) + 130; OutputStream outStream = clientSocket.getOutputStream(); outStream.write(value); } catch (IOException e) { Log.d("BLUETOOTH", e.getMessage()); } break; case R.id.connect: runOnUiThread(new Runnable() { //ui ΠΏΠΎΡ‚ΠΎΠΊ Π² Π³Π»Π°Π²Π½ΠΎΠΌ ΠΏΠΎΡ‚ΠΎΠΊΠ΅ @Override public void run() { button.startAnimation(animation); } }); tryConnectToWatch(); //инициализация bluetooth модуля (Π½Π΅ смартфона) Toast.makeText(this, "ΠŸΠΎΠ΄ΠΎΠΆΠ΄ΠΈΡ‚Π΅...", Toast.LENGTH_LONG); break; } } 

But the idea does not happen. It is first initialized (seconds 5), and then after the initialization is completed, the animation starts.

Why? After all, a separate thread.

  • one
    Android has special classes for working with multithreading. Look at the Handler AsyncTask , they are better suited than a simple (raw) thread or runOnUiThread . For good, you just need to create an instance of AsyncTask by pressing the button and process all your actions in it. - Silento

4 answers 4

If you initialize bluetooth in the UI stream, this means that the animation and the initialization operation will be performed alternately. Judging your code, it will first work out the tryConnectToWatch method, and then the animation. If you want these two operations to work at the same time Try the following code:

 button.startAnimation(animation); new Thread(new Runnable() { @Override public void run() { tryConnectToWatch(); } }).start(); Toast.makeText(this, "ΠŸΠΎΠ΄ΠΎΠΆΠ΄ΠΈΡ‚Π΅...", Toast.LENGTH_LONG); 

If the initialization of the Bluetooth connection does not require work in the UI stream, this code should work.

  • Thank you, as I myself did not think ... I'll take a note! - Ker_ Jen

The fact is that you do not run the animation in a separate thread. You run it in the same stream where you launch your bluetooth. Why is that ? Because in the main thread everything works on the principle of event loops, you just put the animation task in the eventup queue, but the current task is running, so your task that you put in the eventup is waiting for the completion of the current task.

According to the idea, the inclusion of the bluetooth device is just what you need to do in a separate stream, while most likely different from the ui stream, i.e. to make through new Thread (new Runnable ()).

    If I’m not mistaken, I need to create a thread like this: first you need to create an instance of the Runnable class:

     Runnable runnable = new Runnable() { public void run() { // ΠΊΠΎΠ΄ } }; 

    Then create an object of the class Thread and in its constructor pass our runnable and call the .start() method on the object:

     Thread thread = new Thread(runnable); thread.start()' 

    You can slightly reduce the code:

     new Thread(new Runnable() { public void run() { //ΠΊΠΎΠ΄ }); } }).start(); 

    You can also use AsynkTask :

     class MyThread extends AsyncTask<Void, Void, Void> { protected void onPreExecute() { super.onPreExecute(); // выполнится Π΄ΠΎ запуска doInBackground } protected Void doInBackground(Void... params) { // наш ΠΊΠΎΠ΄ Π² ΠΏΠΎΡ‚ΠΎΠΊΠ΅ return null; } protected void onPostExecute(Void result) { super.onPostExecute(result); // выполнится послС doInBackground } } 

    Call:

     MyThread m = new MyThread(); m.execute(); 

      You need to make your connection to the bracelet in a separate thread, you are now basically there. If you do this then the animation will not fail, just keep in mind that you cannot change your views from the stream textView.setText ("The device is connected"); so you need to use something either AsyncTask (well, that comes from the box) or RxJava, a good library of which is convenient to organize multithreading. Sample Code with AT

       class YourTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground() { tryConnectToWatch(); // Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΡƒΠ±Π΅Ρ€ΠΈΡ‚Π΅ с этого ΠΌΠ΅Ρ‚ΠΎΠ΄Π° всС обращСния ΠΊ вьюшкам ΠΈ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ всСх тостов return null; } @Override protected void onPostExecute() { super.onPostExecute(result); // ΠΊΠΎΠ½Π΅Ρ† выполнСния, здСсь Π²Ρ‹ ΡƒΠΆΠ΅ ΠΌΠΎΠΆΠ΅Ρ‚Π΅ ΠΏΠΎΡΡ‚Π°Π²ΠΈΡ‚ΡŒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ‹ // Π²ΠΎ вьюшки Ρ‚Π°ΠΊ ΠΊΠ°ΠΊ этот ΠΌΠ΅Ρ‚ΠΎΠ΄ Π² ΠΎΡ‚Π»ΠΈΡ‡ΠΈΠΈ ΠΎΡ‚ doInBackground() Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ Π½Π° main thread } }