Who can help stop the flow?

to method: void getTrackMetaData(String mLinkToChannel)

transmitted (from StationSwitcher ) link to the online stream (radio station). This method makes a request every 3 seconds and pulls out from the stream data about the song that is playing. Moreover, the TextView with the track information is immediately updated in the same method: titleTextView.post(...)

Accordingly, if you turn on one station and do not switch, then everything works well). The TextView displays, and if the track changes, the track information is updated. However, if you switch the station, i.e. run the method again (from StationSwitcher ) with the new link, then the old stream works (with the old link) and a new one is created.

The question is how to stop the old flow?

 private void getTrackMetaData(String mLinkToChannel) { final Utils.TrackData[] trackData = new Utils.TrackData[1]; Thread thread = new Thread(new Runnable() { @Override public void run() { while (!Thread.currentThread().isInterrupted()) { try { Thread.sleep(3000); try { URL url = new URL(mLinkToChannel) ParsingHeaderData streaming = new ParsingHeaderData(); trackData[0] = streaming.getTrackDetails(url); artistTextView.post(new Runnable() { @Override public void run() { artistTextView.setText( ((trackData[0].artist).equals("")) ? mTitleGag : trackData[0].artist); } }); titleTextView.post(new Runnable() { @Override public void run() { titleTextView.setText( ((trackData[0].title).equals("")) ? " " : trackData[0].title); } }); } catch (Exception e) {e.printStackTrace();} } catch (InterruptedException x) {e.printStackTrace(); } catch (Exception e) {e.printStackTrace();} } } }); thread.start(); thread.interrupt(); } 

p / s

Utils.TrackData is a class in which two String fields: artist and title .

ParsingHeaderData is a class that climbs into a stream and pulls out an object of type Utils.TrackData .

    1 answer 1

    The problem solving code is here.
    http://pastebin.com/AmteatPr

     public class TrackInfoUpdator { private static final String DEFAULT_TILE = "default_title"; private static final String DEFAULT_ARTIST = "default_artist"; public static void demo() { Callback callback = (title, artist) -> { titleTextView.setText(title); artistTextView.setText(artist); }; // this is field of your activity/fragment TrackInfoUpdator updator = new TrackInfoUpdator(callback); // somewhere in onResume() updator.startTracking("anyLink here"); // somewhere in onPause() updator.stopTracking(); } private final Handler uiHandler; private final Callback callback; // not marked as volatile, used only from UI thread private UpdatorThread backgroundThread; public TrackInfoUpdator(Callback callback) { this.uiHandler = new Handler(Looper.getMainLooper()); this.callback = callback; } @UiThread public void startTracking(String link) { stopTracking(); backgroundThread = new UpdatorThread(uiHandler, callback, link); backgroundThread.start(); } @UiThread public void stopTracking() { if (backgroundThread != null) { backgroundThread.disable(); backgroundThread = null; } } private static class UpdatorThread extends Thread { private static final int MSECS_REPEAT = 3000; private final Handler callbackThread; private final Callback callback; private final String link; private volatile boolean isDisabled = false; public UpdatorThread(Handler callbackThread, Callback callback, String link) { this.callbackThread = callbackThread; this.callback = callback; this.link = link; } public void disable() { isDisabled = true; } @Override public void run() { while (!isDisabled) { try { waitForRepeat(); } catch (Exception ignored) { } try { fetchData(); } catch (Exception ignored) { } } } private void waitForRepeat() throws Exception { Thread.sleep(MSECS_REPEAT); } private void fetchData() throws Exception { URL url = new URL(link); ParsingHeaderData streaming = new ParsingHeaderData(); Utils.TrackData trackData = streaming.getTrackDetails(url); String title = TextUtils.isEmpty(trackData.title) ? DEFAULT_TITLE : trackData.title; String artist = TextUtils.isEmpty(trackData.artist) ? DEFAULT_ARTIST : trackData.artist; callbackThread.post(() -> { if (!isDisabled) { callback.onTrackInfoUpdated(title, artist); } }); } } public interface Callback { void onTrackInfoUpdated(String title, String artist); } }