Attaching code to a thread to get proper response from android device
<pre><code>public class MyPlayer extends Activity implements OnClickListener {
/** Called when the activity is first created. */

MediaPlayer mediaPlayer = new MediaPlayer();
Button b1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    b1=(Button)findViewById(R.id.play);
    b1.setOnClickListener(this);
}

public void onClick(View v) {
    // TODO Auto-generated method stub


    if(v.equals(b1))
    {
        String url = "http://108.166.161.206:8826/;stream.mp3";
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaPlayer.setDataSource(url);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            Toast.makeText(this, "data error 1", Toast.LENGTH_LONG).show();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            Toast.makeText(this, "data error 2", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Toast.makeText(this, "data error 3", Toast.LENGTH_LONG).show();
        }
        try {
            mediaPlayer.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mediaPlayer.start();

    }


    }

}
</code></pre>

<p>this is a code to stream audio via http. I have already posted this code that it does not run on device. But no one here could give me a proper solution. However I have found a possible solution to this on developers.android.com</p>

<p>"the call to prepare() can take a long time to execute, because it might involve fetching and decoding media data. So, as is the case with any method that may take long to execute, you should <strong>never call it from your application's UI thread. Doing that will cause the UI to hang until the method returns, which is a very bad user experience and can cause an ANR (Application Not Responding) error.</strong> Even if you expect your resource to load quickly, remember that anything that takes more than a tenth of a second to respond in the UI will cause a noticeable pause and will give the user the impression that your application is slow.</p>

<p>To avoid hanging your UI thread, spawn another thread to prepare the MediaPlayer and notify the main thread when done. However, while you could write the threading logic yourself, this pattern is so common when using MediaPlayer that the framework supplies a convenient way to accomplish this task by using the prepareAsync() method."</p>

<p>Now can anyone please reply how to attach this code to a thread other than the main thread.?
Please reply because I think all over internet this is the only site where I can get a proper reply to this situation and I hope someone would really help me with an example code to threading.</p>
