0

I have a three class: MainActivity, MyService, MyPlayer. MainActivity start service, which start AsyncTask and start MyPlayer (MediaPlayer) play method. But problem that I try to start a radio stream, which are loading for a while(5-10 sec), I want to inform listener that stream loading and infrom when stream loaded (public void onPrepared(MediaPlayer mp) method) in a MainActivity's view, but have an error "can't create handler inside thread". How do I can access to mainactivity's view and context (for toast messages). My code chains:

public class MainActivity extends Activity
 startService(new Intent(this, MyService.class));

public class MyService extends Service 
player = new MyPlayer(ctx);
AsyncTask
protected Void doInBackground(Void... params) {
             player.play();

public class MyPlayer implements OnPreparedListener, OnCompletionListener 
@Override
    public void onPrepared(MediaPlayer mp)

How to get access to MainActivity's view and context from onPrepared?

socm_
  • 783
  • 11
  • 28

1 Answers1

0

This is complex. I think the right approach to your question is here:

How to make a callback from a Service to an Activity

But you may be just fine sending a broadcast:

http://androidexperinz.wordpress.com/2012/02/14/communication-between-service-and-activity-part-1/

Depends on the complexity and timing of the interaction.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36