1

I have a ListView where each row represents content that needs to be upload to a server. Each row contains a Button that when pressed starts an intent service to begin upload to the server.

When first time I press a Button, the intent service starts, but the second time a new intent service does not start ? Should it ? This is the code in onClickListener for my ListView Button.

Intent intent = new Intent(VaultActivity.this, Upload.class);
intent.putExtra(FILEPATH, vidoObject.filePath);
intent.putExtra(POSITION, position);
ListActivity.this.startService(intent);

Am I doing anything wrong? Should the second row button not also create a new IntentService to begin upload ?

4

1 回答 1

3

An IntentService is actually just a work queue:

  • when the first startService call is made the service is started, creating a worker thread and adding the delivered intent to a workqueue
  • the worker thread reads the first intent from the queue and processes it
  • if during processing of the first intent a second intent gets delivered to the service (via a second startService call) that intent gets added to the queue (same for third, fourth etc)
  • the worker keeps running until the work queue is empty, after which the service is stopped

So it may be possible that your second row button starts another service, but only if the first upload was already finished.

If you want to upload two files simultaneously you cannot use an IntentService, because it processes one command at a time.

于 2012-08-30T13:03:08.217 回答