AsyncTask contains a pool of threads (per default, a pool of only one thread, though). You can use AsyncTask to coordinate submitting jobs to the threads and to get results back from the threads. AsyncTask does not anything you could do yourself, but it makes thread- management much easier.
I have not looked in the source-code of AsyncTask, but i suspect it's using ExecutorServices internally... On your question: You should create and close your connection inside your AsyncTask. Best is just to do everything, open --> read --> close, in your doInBackground method. When you create/open your connection in doInBackground, you can store the connection in a member-variable of your AsyncTask instance (be sure that your thread-pool has only one thread, the default). Then in 'onCancelled()' you can get the open connection and forcefully close it. Be sure that when reading data from this connection, to handle io- exception or interrupt-exception gracefully. And when your doInBackground() method closes the connection, it can set this member-variable back to null. Then 'onCancelled()' can check for this member-variable being null: Forcefully close only if it is not null. On Jul 1, 8:48 pm, Brad Chou <[email protected]> wrote: > 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 -- 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 -~----------~----~----~----~------~----~------~--~---

