Android:如何定期更改TextView

时间:2013-07-22 10:31:07

标签: android string updates shoutcast

我最近正致力于ShoutCast广播项目。通过使用streamscrapper库,我能够获得当前歌曲名称和当前艺术家。这是问题所在。一旦我打开应用程序,它就会获得歌曲名称和艺术家名称,但从那以后它就不会更新它。我试着用计时器做但却想不通。

这是我获得当前歌曲ID的方法:

private void initializeMediaPlayer() {

    player = new AACPlayer();
    scraper = new ShoutCastScraper();
    try {
        streams = scraper.scrape(new URI("http://sunucu2.radyolarburada.com:5000/"));
        String str = "asd";
        for (Stream stream: streams) {
            str = stream.getCurrentSong();
            currPlayView.setText("Now Playing: " + str);
        }
    } catch (ScrapeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

2 个答案:

答案 0 :(得分:0)

也许像runnable:

private Handler handler = new Handler();

handler.postAtTime(timeTask, SystemClock.uptimeMillis() + 500);

private Runnable timeTask = new Runnable() {
    public void run() {
        //do stuff here

        //do it again soon
        handler.postAtTime(timeTask, SystemClock.uptimeMillis() + 500);
    }
};

在你离开之前一定要停止任务:

handler.removeCallbacksAndMessages(null);

答案 1 :(得分:0)

使用Handler进行更新,

Handler handler=new Handler();
int FREQ=5000; // the update frequency
handler.postDelayed(new Runnable()
        {
            public void run() 
            {                                 
                try 
                {
                      String currStr;     // get your next song name
                      currPlayView.setText(currStr);    
                } 
                finally 
                {
                    handler.postDelayed(this, FREQ);
                }
            }
        }, FREQ);