Scheduler not updating textview periodically The Next CEO of Stack OverflowHow do I center text horizontally and vertically in a TextView?Is it possible to have multiple styles inside a TextView?Android: combining text & image on a Button or ImageButtonMaking TextView scrollable on AndroidHow do I make links in a TextView clickable?How do I put a border around an Android textview?How to set the text color of TextView in code?Auto Scale TextView Text to Fit within BoundsSet TextView style (bold or italic)Update TextView during long Thread process

Make solar eclipses exceedingly rare, but still have new moons

A small doubt about the dominated convergence theorem

Bartok - Syncopation (1): Meaning of notes in between Grand Staff

Can you be charged for obstruction for refusing to answer questions?

Yu-Gi-Oh cards in Python 3

Is there a difference between "Fahrstuhl" and "Aufzug"

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

Why does the flight controls check come before arming the autobrake on the A320?

Is it ever safe to open a suspicious HTML file (e.g. email attachment)?

Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?

A Man With a Stainless Steel Endoskeleton (like The Terminator) Fighting Cloaked Aliens Only He Can See

Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?

Why is information "lost" when it got into a black hole?

What did we know about the Kessel run before the prequels?

Is it professional to write unrelated content in an almost-empty email?

How to write a definition with variants?

Would a completely good Muggle be able to use a wand?

Are police here, aren't itthey?

Grabbing quick drinks

Why do remote US companies require working in the US?

Do I need to write [sic] when a number is less than 10 but isn't written out?

Is the D&D universe the same as the Forgotten Realms universe?

Rotate a column

Where do students learn to solve polynomial equations these days?



Scheduler not updating textview periodically



The Next CEO of Stack OverflowHow do I center text horizontally and vertically in a TextView?Is it possible to have multiple styles inside a TextView?Android: combining text & image on a Button or ImageButtonMaking TextView scrollable on AndroidHow do I make links in a TextView clickable?How do I put a border around an Android textview?How to set the text color of TextView in code?Auto Scale TextView Text to Fit within BoundsSet TextView style (bold or italic)Update TextView during long Thread process










0















I am trying to update the text in textview from SchedulerExecutiveService. But strangely, textview is updated only the first time and not after that.



ScheduledExecutorService textService = Executors.newScheduledThreadPool(1);
textService.scheduleAtFixedRate(new Runnable()
@Override
public void run()
Log.d("sg", "Logging Seconds");
textStart.setText("Dyanamic text here.");

, 0, 1, TimeUnit.SECONDS);


This code is working properly if I remove textStart.setText(..) part.










share|improve this question



















  • 1





    why are you using Executors for such thing? it is like using a cannon to kill a fly... why dont you use Handlers?

    – pskink
    Oct 20 '15 at 7:33











  • @pskink: Not sure whether or not we can use Handler for performing periodic task.

    – Shubham
    Oct 20 '15 at 7:46











  • see postDelayed method

    – pskink
    Oct 20 '15 at 7:52






  • 1





    IMO, it could be easily done with RxJava, RxAndroid and interval() operator. Most of modern Android apps already have dependency to these libraries, because they're useful for asynchronous operations. As @pskink aleready said, Executors are too heavy for this and you have to deal with concurrency issues by yourself. You can do this if you really understand it or have a lot of time.

    – piotr.wittchen
    Oct 20 '15 at 10:24






  • 1





    @Shubham Check out this article: blog.freeside.co/2015/01/29/… . In your case, you should subscribe on Schedulers.io() thread and observe on AndroidSchedulers.mainThread(). You can adjust sample in this article to your needs and update TextView every given time interval.

    – piotr.wittchen
    Oct 20 '15 at 11:29















0















I am trying to update the text in textview from SchedulerExecutiveService. But strangely, textview is updated only the first time and not after that.



ScheduledExecutorService textService = Executors.newScheduledThreadPool(1);
textService.scheduleAtFixedRate(new Runnable()
@Override
public void run()
Log.d("sg", "Logging Seconds");
textStart.setText("Dyanamic text here.");

, 0, 1, TimeUnit.SECONDS);


This code is working properly if I remove textStart.setText(..) part.










share|improve this question



















  • 1





    why are you using Executors for such thing? it is like using a cannon to kill a fly... why dont you use Handlers?

    – pskink
    Oct 20 '15 at 7:33











  • @pskink: Not sure whether or not we can use Handler for performing periodic task.

    – Shubham
    Oct 20 '15 at 7:46











  • see postDelayed method

    – pskink
    Oct 20 '15 at 7:52






  • 1





    IMO, it could be easily done with RxJava, RxAndroid and interval() operator. Most of modern Android apps already have dependency to these libraries, because they're useful for asynchronous operations. As @pskink aleready said, Executors are too heavy for this and you have to deal with concurrency issues by yourself. You can do this if you really understand it or have a lot of time.

    – piotr.wittchen
    Oct 20 '15 at 10:24






  • 1





    @Shubham Check out this article: blog.freeside.co/2015/01/29/… . In your case, you should subscribe on Schedulers.io() thread and observe on AndroidSchedulers.mainThread(). You can adjust sample in this article to your needs and update TextView every given time interval.

    – piotr.wittchen
    Oct 20 '15 at 11:29













0












0








0








I am trying to update the text in textview from SchedulerExecutiveService. But strangely, textview is updated only the first time and not after that.



ScheduledExecutorService textService = Executors.newScheduledThreadPool(1);
textService.scheduleAtFixedRate(new Runnable()
@Override
public void run()
Log.d("sg", "Logging Seconds");
textStart.setText("Dyanamic text here.");

, 0, 1, TimeUnit.SECONDS);


This code is working properly if I remove textStart.setText(..) part.










share|improve this question
















I am trying to update the text in textview from SchedulerExecutiveService. But strangely, textview is updated only the first time and not after that.



ScheduledExecutorService textService = Executors.newScheduledThreadPool(1);
textService.scheduleAtFixedRate(new Runnable()
@Override
public void run()
Log.d("sg", "Logging Seconds");
textStart.setText("Dyanamic text here.");

, 0, 1, TimeUnit.SECONDS);


This code is working properly if I remove textStart.setText(..) part.







android textview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 16:59









Cœur

19.1k9114155




19.1k9114155










asked Oct 20 '15 at 7:18









ShubhamShubham

1,37431836




1,37431836







  • 1





    why are you using Executors for such thing? it is like using a cannon to kill a fly... why dont you use Handlers?

    – pskink
    Oct 20 '15 at 7:33











  • @pskink: Not sure whether or not we can use Handler for performing periodic task.

    – Shubham
    Oct 20 '15 at 7:46











  • see postDelayed method

    – pskink
    Oct 20 '15 at 7:52






  • 1





    IMO, it could be easily done with RxJava, RxAndroid and interval() operator. Most of modern Android apps already have dependency to these libraries, because they're useful for asynchronous operations. As @pskink aleready said, Executors are too heavy for this and you have to deal with concurrency issues by yourself. You can do this if you really understand it or have a lot of time.

    – piotr.wittchen
    Oct 20 '15 at 10:24






  • 1





    @Shubham Check out this article: blog.freeside.co/2015/01/29/… . In your case, you should subscribe on Schedulers.io() thread and observe on AndroidSchedulers.mainThread(). You can adjust sample in this article to your needs and update TextView every given time interval.

    – piotr.wittchen
    Oct 20 '15 at 11:29












  • 1





    why are you using Executors for such thing? it is like using a cannon to kill a fly... why dont you use Handlers?

    – pskink
    Oct 20 '15 at 7:33











  • @pskink: Not sure whether or not we can use Handler for performing periodic task.

    – Shubham
    Oct 20 '15 at 7:46











  • see postDelayed method

    – pskink
    Oct 20 '15 at 7:52






  • 1





    IMO, it could be easily done with RxJava, RxAndroid and interval() operator. Most of modern Android apps already have dependency to these libraries, because they're useful for asynchronous operations. As @pskink aleready said, Executors are too heavy for this and you have to deal with concurrency issues by yourself. You can do this if you really understand it or have a lot of time.

    – piotr.wittchen
    Oct 20 '15 at 10:24






  • 1





    @Shubham Check out this article: blog.freeside.co/2015/01/29/… . In your case, you should subscribe on Schedulers.io() thread and observe on AndroidSchedulers.mainThread(). You can adjust sample in this article to your needs and update TextView every given time interval.

    – piotr.wittchen
    Oct 20 '15 at 11:29







1




1





why are you using Executors for such thing? it is like using a cannon to kill a fly... why dont you use Handlers?

– pskink
Oct 20 '15 at 7:33





why are you using Executors for such thing? it is like using a cannon to kill a fly... why dont you use Handlers?

– pskink
Oct 20 '15 at 7:33













@pskink: Not sure whether or not we can use Handler for performing periodic task.

– Shubham
Oct 20 '15 at 7:46





@pskink: Not sure whether or not we can use Handler for performing periodic task.

– Shubham
Oct 20 '15 at 7:46













see postDelayed method

– pskink
Oct 20 '15 at 7:52





see postDelayed method

– pskink
Oct 20 '15 at 7:52




1




1





IMO, it could be easily done with RxJava, RxAndroid and interval() operator. Most of modern Android apps already have dependency to these libraries, because they're useful for asynchronous operations. As @pskink aleready said, Executors are too heavy for this and you have to deal with concurrency issues by yourself. You can do this if you really understand it or have a lot of time.

– piotr.wittchen
Oct 20 '15 at 10:24





IMO, it could be easily done with RxJava, RxAndroid and interval() operator. Most of modern Android apps already have dependency to these libraries, because they're useful for asynchronous operations. As @pskink aleready said, Executors are too heavy for this and you have to deal with concurrency issues by yourself. You can do this if you really understand it or have a lot of time.

– piotr.wittchen
Oct 20 '15 at 10:24




1




1





@Shubham Check out this article: blog.freeside.co/2015/01/29/… . In your case, you should subscribe on Schedulers.io() thread and observe on AndroidSchedulers.mainThread(). You can adjust sample in this article to your needs and update TextView every given time interval.

– piotr.wittchen
Oct 20 '15 at 11:29





@Shubham Check out this article: blog.freeside.co/2015/01/29/… . In your case, you should subscribe on Schedulers.io() thread and observe on AndroidSchedulers.mainThread(). You can adjust sample in this article to your needs and update TextView every given time interval.

– piotr.wittchen
Oct 20 '15 at 11:29












4 Answers
4






active

oldest

votes


















1














You can do this way:



public class MainActivity extends Activity 

private TextView textStart;
private int i= 0;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textStart = (TextView)findViewById(R.id.textStart);

ScheduledExecutorService textService = Executors.newScheduledThreadPool(1);
textService.scheduleAtFixedRate(new Runnable()
@Override
public void run()
Log.i("sg", "Logging Seconds");
textStart.post(new Runnable()
@Override
public void run()
i++;
textStart.setText(""+i);

);

, 0, 1, TimeUnit.SECONDS);




It's working fine for me.






share|improve this answer























  • Accepting this, as this answers my original question. As mentioned in comments, using ScheduledExecutorService is definitely one of the worst ways of doing what I intended to do. I did it using RxJava. Please check my answer below for the code.

    – Shubham
    Oct 21 '15 at 8:56


















0














Wrap textStart.setText("Dyanamic text here."); line inside runOnUiThread because scheduleAtFixedRate Runnable called on background Thread from where not possible to access UI elements.






share|improve this answer























  • But then why is it able to update the text in TextView the first time.

    – Shubham
    Oct 20 '15 at 7:24











  • @Shubham: Try it with runOnUiThread

    – ρяσѕρєя K
    Oct 20 '15 at 7:25


















0














You must update your textview in runonUiThread.



I have used Timer to update my textview. Try this, it will help you.



new Timer().schedule(new TimerTask() 

@Override
public void run()

mActivity.runOnUiThread(new Runnable()

@Override
public void run()
// TODO Auto-generated method stub
timertap.setText(timer_count + "");

);



, 1000,1000);





share|improve this answer
































    0














    I finally achieved the same result in a very light weight manner using Rx. Check the code below.



    subscription = Observable
    .interval(0, 1, TimeUnit.SECONDS, Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Observer<Long>()
    @Override
    public void onCompleted()



    @Override
    public void onError(Throwable e)



    @Override
    public void onNext(Long aLong)
    int millis = mediaPlayer.getCurrentPosition();
    String time = String.format("%d:%d sec",
    TimeUnit.MILLISECONDS.toMinutes(millis),
    TimeUnit.MILLISECONDS.toSeconds(millis) -
    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
    );
    textStart.setText(time);

    );





    share|improve this answer























    • Can be done in fewer lines of code using lambdas.

      – Shubham
      Oct 21 '15 at 8:58











    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33230328%2fscheduler-not-updating-textview-periodically%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can do this way:



    public class MainActivity extends Activity 

    private TextView textStart;
    private int i= 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textStart = (TextView)findViewById(R.id.textStart);

    ScheduledExecutorService textService = Executors.newScheduledThreadPool(1);
    textService.scheduleAtFixedRate(new Runnable()
    @Override
    public void run()
    Log.i("sg", "Logging Seconds");
    textStart.post(new Runnable()
    @Override
    public void run()
    i++;
    textStart.setText(""+i);

    );

    , 0, 1, TimeUnit.SECONDS);




    It's working fine for me.






    share|improve this answer























    • Accepting this, as this answers my original question. As mentioned in comments, using ScheduledExecutorService is definitely one of the worst ways of doing what I intended to do. I did it using RxJava. Please check my answer below for the code.

      – Shubham
      Oct 21 '15 at 8:56















    1














    You can do this way:



    public class MainActivity extends Activity 

    private TextView textStart;
    private int i= 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textStart = (TextView)findViewById(R.id.textStart);

    ScheduledExecutorService textService = Executors.newScheduledThreadPool(1);
    textService.scheduleAtFixedRate(new Runnable()
    @Override
    public void run()
    Log.i("sg", "Logging Seconds");
    textStart.post(new Runnable()
    @Override
    public void run()
    i++;
    textStart.setText(""+i);

    );

    , 0, 1, TimeUnit.SECONDS);




    It's working fine for me.






    share|improve this answer























    • Accepting this, as this answers my original question. As mentioned in comments, using ScheduledExecutorService is definitely one of the worst ways of doing what I intended to do. I did it using RxJava. Please check my answer below for the code.

      – Shubham
      Oct 21 '15 at 8:56













    1












    1








    1







    You can do this way:



    public class MainActivity extends Activity 

    private TextView textStart;
    private int i= 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textStart = (TextView)findViewById(R.id.textStart);

    ScheduledExecutorService textService = Executors.newScheduledThreadPool(1);
    textService.scheduleAtFixedRate(new Runnable()
    @Override
    public void run()
    Log.i("sg", "Logging Seconds");
    textStart.post(new Runnable()
    @Override
    public void run()
    i++;
    textStart.setText(""+i);

    );

    , 0, 1, TimeUnit.SECONDS);




    It's working fine for me.






    share|improve this answer













    You can do this way:



    public class MainActivity extends Activity 

    private TextView textStart;
    private int i= 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textStart = (TextView)findViewById(R.id.textStart);

    ScheduledExecutorService textService = Executors.newScheduledThreadPool(1);
    textService.scheduleAtFixedRate(new Runnable()
    @Override
    public void run()
    Log.i("sg", "Logging Seconds");
    textStart.post(new Runnable()
    @Override
    public void run()
    i++;
    textStart.setText(""+i);

    );

    , 0, 1, TimeUnit.SECONDS);




    It's working fine for me.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 20 '15 at 7:25









    Hiren PatelHiren Patel

    37.5k14138121




    37.5k14138121












    • Accepting this, as this answers my original question. As mentioned in comments, using ScheduledExecutorService is definitely one of the worst ways of doing what I intended to do. I did it using RxJava. Please check my answer below for the code.

      – Shubham
      Oct 21 '15 at 8:56

















    • Accepting this, as this answers my original question. As mentioned in comments, using ScheduledExecutorService is definitely one of the worst ways of doing what I intended to do. I did it using RxJava. Please check my answer below for the code.

      – Shubham
      Oct 21 '15 at 8:56
















    Accepting this, as this answers my original question. As mentioned in comments, using ScheduledExecutorService is definitely one of the worst ways of doing what I intended to do. I did it using RxJava. Please check my answer below for the code.

    – Shubham
    Oct 21 '15 at 8:56





    Accepting this, as this answers my original question. As mentioned in comments, using ScheduledExecutorService is definitely one of the worst ways of doing what I intended to do. I did it using RxJava. Please check my answer below for the code.

    – Shubham
    Oct 21 '15 at 8:56













    0














    Wrap textStart.setText("Dyanamic text here."); line inside runOnUiThread because scheduleAtFixedRate Runnable called on background Thread from where not possible to access UI elements.






    share|improve this answer























    • But then why is it able to update the text in TextView the first time.

      – Shubham
      Oct 20 '15 at 7:24











    • @Shubham: Try it with runOnUiThread

      – ρяσѕρєя K
      Oct 20 '15 at 7:25















    0














    Wrap textStart.setText("Dyanamic text here."); line inside runOnUiThread because scheduleAtFixedRate Runnable called on background Thread from where not possible to access UI elements.






    share|improve this answer























    • But then why is it able to update the text in TextView the first time.

      – Shubham
      Oct 20 '15 at 7:24











    • @Shubham: Try it with runOnUiThread

      – ρяσѕρєя K
      Oct 20 '15 at 7:25













    0












    0








    0







    Wrap textStart.setText("Dyanamic text here."); line inside runOnUiThread because scheduleAtFixedRate Runnable called on background Thread from where not possible to access UI elements.






    share|improve this answer













    Wrap textStart.setText("Dyanamic text here."); line inside runOnUiThread because scheduleAtFixedRate Runnable called on background Thread from where not possible to access UI elements.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 20 '15 at 7:23









    ρяσѕρєя Kρяσѕρєя K

    115k26162188




    115k26162188












    • But then why is it able to update the text in TextView the first time.

      – Shubham
      Oct 20 '15 at 7:24











    • @Shubham: Try it with runOnUiThread

      – ρяσѕρєя K
      Oct 20 '15 at 7:25

















    • But then why is it able to update the text in TextView the first time.

      – Shubham
      Oct 20 '15 at 7:24











    • @Shubham: Try it with runOnUiThread

      – ρяσѕρєя K
      Oct 20 '15 at 7:25
















    But then why is it able to update the text in TextView the first time.

    – Shubham
    Oct 20 '15 at 7:24





    But then why is it able to update the text in TextView the first time.

    – Shubham
    Oct 20 '15 at 7:24













    @Shubham: Try it with runOnUiThread

    – ρяσѕρєя K
    Oct 20 '15 at 7:25





    @Shubham: Try it with runOnUiThread

    – ρяσѕρєя K
    Oct 20 '15 at 7:25











    0














    You must update your textview in runonUiThread.



    I have used Timer to update my textview. Try this, it will help you.



    new Timer().schedule(new TimerTask() 

    @Override
    public void run()

    mActivity.runOnUiThread(new Runnable()

    @Override
    public void run()
    // TODO Auto-generated method stub
    timertap.setText(timer_count + "");

    );



    , 1000,1000);





    share|improve this answer





























      0














      You must update your textview in runonUiThread.



      I have used Timer to update my textview. Try this, it will help you.



      new Timer().schedule(new TimerTask() 

      @Override
      public void run()

      mActivity.runOnUiThread(new Runnable()

      @Override
      public void run()
      // TODO Auto-generated method stub
      timertap.setText(timer_count + "");

      );



      , 1000,1000);





      share|improve this answer



























        0












        0








        0







        You must update your textview in runonUiThread.



        I have used Timer to update my textview. Try this, it will help you.



        new Timer().schedule(new TimerTask() 

        @Override
        public void run()

        mActivity.runOnUiThread(new Runnable()

        @Override
        public void run()
        // TODO Auto-generated method stub
        timertap.setText(timer_count + "");

        );



        , 1000,1000);





        share|improve this answer















        You must update your textview in runonUiThread.



        I have used Timer to update my textview. Try this, it will help you.



        new Timer().schedule(new TimerTask() 

        @Override
        public void run()

        mActivity.runOnUiThread(new Runnable()

        @Override
        public void run()
        // TODO Auto-generated method stub
        timertap.setText(timer_count + "");

        );



        , 1000,1000);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 20 '15 at 10:33

























        answered Oct 20 '15 at 8:52









        Harish VatsHarish Vats

        522318




        522318





















            0














            I finally achieved the same result in a very light weight manner using Rx. Check the code below.



            subscription = Observable
            .interval(0, 1, TimeUnit.SECONDS, Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<Long>()
            @Override
            public void onCompleted()



            @Override
            public void onError(Throwable e)



            @Override
            public void onNext(Long aLong)
            int millis = mediaPlayer.getCurrentPosition();
            String time = String.format("%d:%d sec",
            TimeUnit.MILLISECONDS.toMinutes(millis),
            TimeUnit.MILLISECONDS.toSeconds(millis) -
            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
            );
            textStart.setText(time);

            );





            share|improve this answer























            • Can be done in fewer lines of code using lambdas.

              – Shubham
              Oct 21 '15 at 8:58















            0














            I finally achieved the same result in a very light weight manner using Rx. Check the code below.



            subscription = Observable
            .interval(0, 1, TimeUnit.SECONDS, Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<Long>()
            @Override
            public void onCompleted()



            @Override
            public void onError(Throwable e)



            @Override
            public void onNext(Long aLong)
            int millis = mediaPlayer.getCurrentPosition();
            String time = String.format("%d:%d sec",
            TimeUnit.MILLISECONDS.toMinutes(millis),
            TimeUnit.MILLISECONDS.toSeconds(millis) -
            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
            );
            textStart.setText(time);

            );





            share|improve this answer























            • Can be done in fewer lines of code using lambdas.

              – Shubham
              Oct 21 '15 at 8:58













            0












            0








            0







            I finally achieved the same result in a very light weight manner using Rx. Check the code below.



            subscription = Observable
            .interval(0, 1, TimeUnit.SECONDS, Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<Long>()
            @Override
            public void onCompleted()



            @Override
            public void onError(Throwable e)



            @Override
            public void onNext(Long aLong)
            int millis = mediaPlayer.getCurrentPosition();
            String time = String.format("%d:%d sec",
            TimeUnit.MILLISECONDS.toMinutes(millis),
            TimeUnit.MILLISECONDS.toSeconds(millis) -
            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
            );
            textStart.setText(time);

            );





            share|improve this answer













            I finally achieved the same result in a very light weight manner using Rx. Check the code below.



            subscription = Observable
            .interval(0, 1, TimeUnit.SECONDS, Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<Long>()
            @Override
            public void onCompleted()



            @Override
            public void onError(Throwable e)



            @Override
            public void onNext(Long aLong)
            int millis = mediaPlayer.getCurrentPosition();
            String time = String.format("%d:%d sec",
            TimeUnit.MILLISECONDS.toMinutes(millis),
            TimeUnit.MILLISECONDS.toSeconds(millis) -
            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
            );
            textStart.setText(time);

            );






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 21 '15 at 8:57









            ShubhamShubham

            1,37431836




            1,37431836












            • Can be done in fewer lines of code using lambdas.

              – Shubham
              Oct 21 '15 at 8:58

















            • Can be done in fewer lines of code using lambdas.

              – Shubham
              Oct 21 '15 at 8:58
















            Can be done in fewer lines of code using lambdas.

            – Shubham
            Oct 21 '15 at 8:58





            Can be done in fewer lines of code using lambdas.

            – Shubham
            Oct 21 '15 at 8:58

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33230328%2fscheduler-not-updating-textview-periodically%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко