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.
HandlerAsyncTask, they are better suited than a simple (raw) thread orrunOnUiThread. For good, you just need to create an instance ofAsyncTaskby pressing the button and process all your actions in it. - Silento