Emulator freezes while downloading
<p>I'm trying to build a simple application to download and play a music file. I made a code but when try to test it, the application freezes and shows <strong>'Launch timeout has expired, giving up wake lock'</strong> If anyone has an idea, would you please give me little advice? Below is my code.</p>

<p>By the way, the sample file I'm trying to download has about 9.1MB size.</p>

<pre><code>public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {
        setDataSource("http://cfs.tistory.com/custom/blog/66/661632/skin/images/Believe.mp3");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private void setDataSource(String path) throws IOException {        

    URL url = new URL(path);
    HttpURLConnection cn = (HttpURLConnection) url.openConnection();
    cn.setRequestMethod("GET");
    cn.setDoOutput(true);
    cn.connect();

    String file_path = Environment.getExternalStorageDirectory()+"/download/";
    String fileName = "Temp.wav";
    File file = new File(file_path);
    file.mkdir();

    InputStream is = cn.getInputStream();
    File output_file = new File(file, fileName);
    FileOutputStream fos = new FileOutputStream(output_file);

    byte[] buffer = new byte[4096];
    int read = 0;

    while ((read = is.read(buffer)) &gt; 0){
        fos.write(buffer, 0, read);
    }
    is.close();
    fos.close();

    mp = new MediaPlayer();
    mp.setDataSource(file_path+fileName);
    mp.prepare();
    mp.start();

}
</code></pre>

<p>}</p>

<p>** Modified: ** Justin taught me if I just put it on onCreate() then it causes a problem. Now I'm using a button with a thread but got another problem that says <strong>only the original thread that created a view hierarchy can touch its views</strong> and seems like it's happening at 'fos.close'. If anyone knows what to do, would you please help me?</p>

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

    tv = (TextView) findViewById(R.id.tv1);
    tv2 = (TextView) findViewById(R.id.tv2);
    tv.setText(Environment.getExternalStorageDirectory()+"/download/");

    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener(){

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Thread thread;
            Runnable r = new Runnable(){
                public void run(){
                    try {
                        setDataSource("http://cfs.tistory.com/custom/blog/66/661632/skin/images/Believe.mp3");
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                       
                }
            };
            thread = new Thread(r);
            thread.start();

        }

    });
}
</code></pre>

<p>** Error Messages: **</p>

<pre><code>
07-15 04:31:45.321: ERROR/AndroidRuntime(313): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
</code></pre>
