Android IntentService的意外行为
我在代码中使用IntentService而不是Service,因为IntentService在onHandleIntent(Intent-Intent)中为我创建了一个线程,所以我不必在服务的代码中创建Thead 我希望两个对同一个IntentService的Intent将并行执行,因为IntentService中会为每个IntentService生成一个线程。但我的代码证明这两个意图是按顺序执行的 这是我的IntentService代码:Android IntentService的意外行为,android,Android,我在代码中使用IntentService而不是Service,因为IntentService在onHandleIntent(Intent-Intent)中为我创建了一个线程,所以我不必在服务的代码中创建Thead 我希望两个对同一个IntentService的Intent将并行执行,因为IntentService中会为每个IntentService生成一个线程。但我的代码证明这两个意图是按顺序执行的 这是我的IntentService代码: public class UpdateService
public class UpdateService extends IntentService {
public static final String TAG = "HelloTestIntentService";
public UpdateService() {
super("News UpdateService");
}
protected void onHandleIntent(Intent intent) {
String userAction = intent
.getStringExtra("userAction");
Log.v(TAG, "" + new Date() + ", In onHandleIntent for userAction = " + userAction + ", thread id = " + Thread.currentThread().getId());
if ("1".equals(userAction)) {
try {
Thread.sleep(20 * 1000);
} catch (InterruptedException e) {
Log.e(TAG, "error", e);
}
Log.v(TAG, "" + new Date() + ", This thread is waked up.");
}
}
}
服务调用代码如下:
public class HelloTest extends Activity {
//@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent selectIntent = new Intent(this, UpdateService.class);
selectIntent.putExtra("userAction",
"1");
this.startService(selectIntent);
selectIntent = new Intent(this, UpdateService.class);
selectIntent.putExtra("userAction",
"2");
this.startService(selectIntent);
}
}
我在日志中看到此日志消息:
V/HelloTestIntentService( 848): Wed May 05 14:59:37 PDT 2010, In onHandleIntent for userAction = 1, thread id = 8
D/dalvikvm( 609): GC freed 941 objects / 55672 bytes in 99ms
V/HelloTestIntentService( 848): Wed May 05 15:00:00 PDT 2010, This thread is waked up.
V/HelloTestIntentService( 848): Wed May 05 15:00:00 PDT 2010, In onHandleIntent for userAction = 2, thread id = 8
I/ActivityManager( 568): Stopping service: com.example.android/.UpdateService
日志显示第二个意图等待第一个意图完成,并且它们在同一个线程中
我对IntentService有什么误解吗。要使两个服务意图并行执行,我是否必须用服务替换IntentService并在服务代码中自己启动一个线程
谢谢。据我所知,IntentService有一个处理程序线程,每个intent都在该线程中排队。完成所有排队意图后,服务退出。它不会根据意图创建独立线程。我不知道有任何服务子类按照您所描述的方式工作,您可能必须自己编写。意图队列是使用IntentService的关键。我认为您想要的是一个异步任务,而不是一个服务或IntentService。或者,您也可以通过定义如下的runnable从臀部开始射击:
Runnable runnable = new Runnable() {
public void run() {
....
}
};
new Thread(runnable).start();
。。。对于您的每项任务。老实说,这可能比处理这些Android helper类更容易。对IntentService的所有请求都在一个工作线程上处理,一次只处理一个请求。若您想并行地完成两个任务,我认为您需要使用服务,并在服务启动后为每个任务创建线程 对于AsyncTask,有一个线程池用于处理所有任务。如果任务号超过线程池大小,则其中一些异步任务将需要等待,直到池中的线程可用。但是,线程池大小在不同的平台版本中会发生变化 以下是我的测试结果:
- Android 2.2:线程池大小=5
- Android 1.5:线程池大小=1
- 这里是一个使用服务而不是IntentService的示例,它可能会满足您的目的 IntentService的设计模式是一个工作队列,它保证请求排队而不是并行处理。正如下面指出的,您可以为每个并行工作启动一个异步任务。确保您的服务代码是可重入的。谢谢您的简短解释。真正有帮助的是,我会在一个可运行的线程上使用AsyncTask——这无疑有助于预处理/后处理,并将结果返回到UI线程。如果任何活动的持续时间都超过活动的生命周期,那么使用IntentService就是一个好办法。AsyncTask很好,但应该用于服务中,您应该始终牢记Android生命周期:在配置更改期间,活动会被销毁并重新创建。。。AsyncTask将失去对活动的引用,可能会被销毁。。。