1

I have an Activity that must constantly get the result either from web or from MP3 stream data (ID3 tag) and display them on its TextViews.

So the parsing can be implemented in Service that plays the stream or in AsyncTask, that parses the file. It must be on timer and load the data only when the Activity became visible.

The parsing by itself can be realized, but what's the way to constantly run this task and update Activity UI?

I've seen some links, tried some own codes, but it worked only for 1 launch. The example for multiple call of AsyncTask from this site didn't work (crash).

Please give a stable simple working example of timered call of AsyncTask with constantly UI update or timered call of Service method with UI update.

As I understand, the timer must be called in UI thread in onCreate and onResume?

Thanks.

UPDATED CODE: (here is the working version of timer that updates UI, need to be tested with AsyncTask work)

doUpdate();   // in the UI onCreate

TimerTask updateTask;
     final Handler handler = new Handler();
     Timer timer = new Timer();

     public void doUpdate(){

     updateTask = new TimerTask() {
                  public void run() {
                  handler.post(new Runnable() {
                                 public void run() {
                                     Random r = new Random();
                                     int nm=r.nextInt(100-1) + 1;
                                     updatePlaylist(String.valueOf(nm));   //here we update the TextView

// BUT CALLING:
// new PlayList(PlayerActivity.this).execute(this); 
// doesn't work! It's an AsyncTask.
                                 }
                        });
                 }};
          timer.schedule(updateTask, 0, 2000);
          }

So maybe new AsyncTask object code line is wrong?

     class PlayList extends AsyncTask<Activity, Void, String> {


    private PlayerActivity act;

    public PlayList(Activity activity) {
        this.act = (PlayerActivity) activity;
    }


    protected String doInBackground(Activity... activities) {


        String result;

        Random r = new Random();
        int num=r.nextInt(100-1) + 1;

        result=String.valueOf(num);
        //result=act.mp3Service.getPlaylist(); // will work later

        return result;

    }


    protected void onPostExecute(String result) {

            act.updatePlaylist(result);


    }


}
Tramway11
  • 419
  • 3
  • 9
  • 20
  • If it only needs to last for the life of the `Activity` then an `AsyncTask` should be fine. Otherwise, a service would probably be better. Post what you have tried and we can help find what's wrong. The timer can be set in any `Activity` method depending on what you need. – codeMagic Jul 12 '13 at 22:12
  • I tried http://stackoverflow.com/questions/6531950/how-to-execute-async-task-repeatedly-after-fixed-time-intervals Strange that it doesn't work although it has 19 votes. – Tramway11 Jul 13 '13 at 13:31
  • Every code is different. I didn't inspect that question well but it is probably good code for what it needs to do. There's no way of saying for sure if it will work for you or what problem you are having without seeing YOUR code and YOUR logcat – codeMagic Jul 13 '13 at 14:27
  • I've updated the code. The timer by itself works, but calling the new AsyncTask in it fails – Tramway11 Jul 13 '13 at 15:40
  • Errors are: Activity has leaked Service connection that was originally bound here:, or an error occured while executing doInBackground() – Tramway11 Jul 13 '13 at 15:51
  • Well, the code seems to be right, maybe I must to sleep in the thread with AsyncTask for getting sure that has been connected to the Service. Need to test if it's stable – Tramway11 Jul 13 '13 at 16:02
  • Hope this code may help someone. – Tramway11 Jul 13 '13 at 16:04

1 Answers1

1

Hard to say what the exact, total solution should be, but I suggest you use a separate thread running in a Service. You may be getting a crash on AsyncTask because you're trying to use the AsyncTask object more than once. You can't do that; you have to create a new object each time you run the background task.

Joe Malin
  • 8,621
  • 1
  • 23
  • 18