Unexpected IntentService Behavior

I used IntentService in my code instead of a service because IntentService creates a thread for me in onHandleIntent (intent intent), so I don’t need to create Thead myself in the code of my service.

I expected that two intentions for one IntentSerivce would be executed in parallel, because a thread is created in the IntentService for each invention. But my code showed that two intentions are executed in a sequential manner.

This is my IntentService code:

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.");
        }
    }
}

And calling the service code below:

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);

    }
}

I saw this log message in a log:

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

The magazine shows that the second intention awaited the first intention to the end, and they are in the same stream.

- IntentService. , IntentService ?

.

+5
5

- IntentService.

+9

IntentService , . , , .

AsyncTask, . , AsyncTasks , . .

:

  • Android 2.2: = 5
  • Android 1.5: = 1
+7

, IntentService , . , . . - , , , , , .

+6

I think you need AsyncTask, not a service or IntentService. Or you can always just shoot from the hip, defining runnable as follows:

Runnable runnable = new Runnable() {

    public void run() {
        ....
    }
};
new Thread(runnable).start();

... for each of your tasks. Honestly, this might be easier than dealing with these Android helper classes.

+1
source

Here is an example of using a service instead of an IntentService, it can serve your purpose. http://developer.android.com/guide/topics/fundamentals/services.html#ExtendingIntentService

0
source

All Articles