I have a class that inherits from Runnable
public class SentZip implements Runnable { @Override public void run() { while (true){ } } and there is a main in which I run this runnable through a handler
public class MainActivity extends AppCompatActivity { Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); handler = new Handler(); } public void send1(View view) { handler.post(new SentZip()); } public void send2(View view) { Thread thread = new Thread(new SentZip()); thread.start(); } And for the test I hung on the ProgressBar screen so that it spins and shows the screen freezes or not ...
And when I launch the send1 method, the screen hangs, and when send2 is not ... It seems that the Handler is running correctly, why does it occupy the main thread?
This is what happens with the Handler.
public void send(View view) { HandlerThread handlerThread = new HandlerThread("one", HandlerThread.NORM_PRIORITY); Looper mLooper = handlerThread.getLooper(); ----> Handler handler = new Handler(mLooper); handler.post(new SentZip()); handlerThread.start(); }