1

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.

4

3 回答 3

1

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.

于 2012-06-10T22:33:41.207 回答
0

在你的网络 jar 库中,如果你使用 package 中的普通旧线程java.lang,你不需要做任何特别的事情,因为它从 API 级别 1 被移植到 Android 中。

如果您使用 package 中的类java.util.concurrent,则需要对网络 jar 库进行第二次尝试,因为某些类自 API 级别 1 以来未移植到 Android,因此可能不适用于所有 API 级别,例如java.util.concurrent。从 API Level 9 开始引入BlockingDeque 。

同样的情况也存在于 packagejava.net中,例如java.net.IDN是从 API Level 9 开始引入的。所以最好在网络 jar 库中检查它们。

于 2012-06-10T23:37:32.107 回答
0
Threads from java works just fine with Android..

但是你必须记住一些事情。

  1. 让非 UI 工作远离 UI 线程是一种很好的编程实践。但

    从 HoneyComb 它是一个规则。

  2. 您可以使用 java 方式,通过将类扩展为Thread class or implementing Runnable interface.

  3. 一旦你在 android 中创建了一个单独的线程,你就会从 UI 线程中删除,所以要返回在你需要使用的 ui 线程上显示非 ui 工作Handler,或者尝试使用AsyncTask(它同步 UI 和非Android中的用户界面)。

于 2012-06-11T05:58:55.130 回答