Yes, I use another thread to open connection and get data. So, if I set a flag to break the while loop and the thread is over, does it mean the connection close ?
I will try to use the AsyncTask, but before that, I want to know the difference between thread and AsyncTask. Can somebody explain that ? Best Regards, Brad On 7月1日, 下午10時09分, Streets Of Boston <[email protected]> wrote: > Like Dimitris said: > Try to call 'cancel(true)' on the AsyncTask. It will invoke the > thread's 'interrupt()' method, making sure that if the thread is in a > wait-state, this wait-state will be interrupted. > > Then in the onCancelled() you may be able to get hold of connections/ > etc from your AsyncTask, used in the 'doInBackground()' method, that > are open and then forcefully close them. > > On Jul 1, 1:51 am, Dimitris <[email protected]> wrote: > > > > > I believe there is a conn.disconnect() and as you mentioned it is > > always good to close() the inputstream. > > > I assume this operation occurs in another thread instead of the UI > > thread. Take a look at the asynctask class offered with 1.5. You can > > call cancel on the thread and raise the onCancelled() event or check > > for isCancelled() boolean in your loop. > > > -dc > > > On Jul 1, 4:09 am, Brad Chou <[email protected]> wrote: > > > > I open a connect like this: > > > > URL mUrl = new URL("http://www.abc.com/4MB.mp3"); > > > URLConnection conn = mUrl.openConnection(); > > > conn.connect(); > > > InputStream is = conn.getInputStream(); > > > > FileOutputStream fos = new FileOutputStream(tmpFile); > > > byte[] readBody = new byte[4096]; > > > do { > > > int readBytes = is.read(readBody); > > > if (readBytes <= 0) { > > > break; > > > } > > > fos.write(readBody, 0, readBytes); > > > > }while(true); > > > > I have two question > > > (1) How to cancel the connection when conn.connect() is executed ? I > > > can not find any useful API from > > > http://developer.android.com/reference/java/net/URLConnection.html > > > (2) When user cancel this activity, I use is.close() to cancel the > > > is.read(). Is that a good way to cancel InputStream ? and how to handle > > > (cancel) the conn this moment ? > > > > Best Regards, > > > Brad- Hide quoted text - > > > - Show quoted text - --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

