How to restart a stream in android?

in my android app. I have a ui update thread that keeps all my views up to date.

protected Thread UIUpdateThread = new Thread()
{
 @Override
 public void run()
 {
  while(true)
  {
    query_some_data_from_service(); // gets some "fresh" data from a service
    UIUpdateHandler.sendEmptyMessage(0); // will update all ui elements with the new values
    sleep(1234);
  }
 }
};

Run this thread in onCreate () with

UIUpdateThread.start();

and everything works just fine :) when I leave the action (for example, because I switch to another action), I call

UIUpdateThread.interrupt();

inside onStop () to prevent the stream from running continuously. if I do not, it will work even if I close the application!?!? !!

Problem

consists in the following: how can I return this stream to life, returning from some other activity to this? run () does not work, calling initial start () in some other method, for example onResume (), the application crashes.

I already tried a lot, but nothing works :(

+3
4

, , .

-, , , , interrupt(), . , , AsyncTask , AsyncTask.

, , - , . , Android. , , , interrupt().

+6

Thread.start() . () , , , .

. ? Android , .

Update:

. . , , , . ResultsReceiver, Activity . , onCreate , . Google I/O 2010 .

+3

Java ( , Android), Thread , .

+2

:

Suggested Methods for Stopping a Thread in HowToStopAThread

0
source

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


All Articles