Getting current activity from thread
I have a thread that listens for a TCP connection. When a connection is made, I would like to basically stop what the current activity is doing, and move to another activity that runs different threads and display a different layout.
For now I would also like to keep that thread running (waiting for a TCP connection).
I can change activity from one to another using startActivity() on an Intent, but that doesn't work while within my开发者_开发技巧 thread.
How can I please achieve that behavior?
Thanks a lot,
James
Rules of thumb of long-running task programming in Android. If you have:
A short-running task (thread) that is only used by one Activity at a time then use AsyncTask.
A long-running task that should survive Activity's lifecycle and/or is used by multiple Activities (also external to your app), than use Service. Service is a separate process that is more resilient to shutdowns than Activity.
A task that needs to be run based on some external condition like: location changed, network available, etc. Or if you need a periodical data sync or a timer based task, then you don't need to have a task running, just register your code with an appropriate API and OS will call it when appropriate. This is called asynchronous programming and is the preferable option on Android, since it minimizes memory and processor use by only invoking code when it needs to be invoked.
精彩评论