Reputation: 43
I wrote a network package for socket i/o which uses normal java threads, and I'm wondering if it is possible to use this package? I'm not manipulating anything on the UI of the Activity with these threads.
Or do I have to port those java threads to Android compatible ones? Because I always thought you can use normal java threads as long as they don't change views on the Activity.
How do you post code samples?
Got connecting working now testing if the Message Queues are working. Im using 2 LinkedBlockingQueue Input and Output. Reason why i need to use Threads. Now i just converted the Class that i use to encapsulate connection and SocketIO to a AsyncTask. This is for a school project where we need to control a vehicle over wifi with a Android app.
Upvotes: 1
Views: 259
Reputation: 33544
Threads from java works just fine with Android..
But you must keep few things in mind.
It was a good programming practice to keep the Non-UI work away from UI thread . But
from HoneyComb its a rule.
You can use the java way, by extending the class to Thread class or implementing Runnable interface.
Once you create a separate thread in android, you are dropped from the UI thread, so to get back to display the non-ui work on the ui thread you need to use either Handler
, or try using the AsyncTask
(It synchronizes the UI and Non-UI in Android).
Upvotes: 0
Reputation: 41126
Inside your network jar library, if you use the plain old thread from package java.lang
, you don't need to do anything special, as it was ported into Android since API Level 1.
If you use classes from package java.util.concurrent
, you need take a second shot on your network jar library, as some classes are not ported into Android since API Level 1, hence may not work on all API Level, for instance, java.util.concurrent.BlockingDeque is introduced since API Level 9.
Same situation exists in package java.net
, for instance, java.net.IDN is introduced since API Level 9. So better to check those in network jar library as well.
Upvotes: 0
Reputation: 7682
You can use Java threads on Android. In fact, they are no different, and the Android model does support the common Thread
class. However, simply taking your old code, sticking some Android specific components on to it, and hoping for the best, is not advised. Instead, this would be a good place to use a Service
to coordinate the background threads that line up with the UI. Services run devoid of the UI, but are in a separate component, to separate them logically from the rest of your app.
Upvotes: 1