Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android the difference between onHandleIntent & onStartCommand

I am currently writing a android program which needs an IntentService. When I put the code in the onHandleIntent function, the code does not run, but it doesn't give errors in the MainActivity. But when I copy my code into the onStartCommand, it runs perfectly.

The problem is that I wanna know what are the differences between onHandleIntent and onStartCommand. Thanks.

CODE:

In onHandleIntent:

System.out.println("SERVICE STARTED! ! !");
//System.out.println(intent.getBooleanExtra("once", Boolean.FALSE));
if (intent.getBooleanExtra("once", Boolean.FALSE)) {
    Check();
}
mHandler.postDelayed(mRunnable, 3000);
like image 234
Chromium Avatar asked Jan 07 '16 02:01

Chromium


People also ask

What is difference between service and IntentService in Android?

Service class uses the application's main thread, while IntentService creates a worker thread and uses that thread to run the service. IntentService creates a queue that passes one intent at a time to onHandleIntent() . Thus, implementing a multi-thread should be made by extending Service class directly.

What is an IntentService in Android?

IntentService is an extension of the Service component class that handles asynchronous requests (expressed as Intent s) on demand. Clients send requests through Context.

Does IntentService run on background thread?

The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface's responsiveness.


1 Answers

As from the docs:

The IntentService does the following:

  • Creates a default worker thread that executes all intents delivered to onStartCommand() separate from your application's main thread.
  • Creates a work queue that passes one intent at a time to your onHandleIntent() implementation, so you never have to worry about multi-threading.
  • Stops the service after all start requests have been handled, so you never have to call stopSelf().
  • Provides default implementation of onBind() that returns null.
  • Provides a default implementation of onStartCommand() that sends the intent to the work queue and then to your onHandleIntent() implementation.

And also:

All this adds up to the fact that all you need to do is implement onHandleIntent() to do the work provided by the client. (Though, you also need to provide a small constructor for the service.)

So an IntentService is a "Custom" Service with those special properties. So there's no need to override the onStartCommand(), actually, you shouldn't do it unless you're using the regular Service class.

Some example of IntentService usage:

Activity.java

Intent it = new Intent(getApplicationContext(), YourIntentService.class);
it.putExtra("Key", "Value");
startService(it);

YourIntentService.java

public YourIntentService() {
    super("YourIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {

    if (intent != null) {
        String str = intent.getStringExtra("key");
        // Do whatever you need to do here.
    }
    //...
}

You can also check this tutorial or this one for more info about Service and IntentService.

Also, check the docs.

like image 131
Mauker Avatar answered Nov 06 '22 19:11

Mauker