-1

To know the difference between IntentService and Service in Android, I created the below posted small test of an IntentService class. The IntentService class can be started using startService(intent); which will result in a call to nStartCommand(Intent intent, int flags, int startId). Also to send values from the IntentService class to the MainActivity for an example, we should send it via sendBroadcast(intent); and the MainActivity should register a broadcastReceiver for that action so it can receive the values sent via

sendBroadcast(intent);

so far I cant see any difference between Service and IntentService!! Since they are similar in the way of starting them and the way they broadcast data,can you please tell me in which context they differ?

please tell me why i am receiving those errors and how to solve it

MainActivity

public class MainActivity extends AppCompatActivity {

    private final String TAG = this.getClass().getSimpleName();

    private Button mbtnSend = null;
    private int i = 0;

    private BroadcastReceiver mBCR_VALUE_SENT = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (action.equals(MyIntentService.INTENT_ACTION)) {
                int intnetValue = intent.getIntExtra(MyIntentService.INTENT_KEY, -1);
                Log.i(TAG, SubTag.bullet("mBCR_VALUE_SENT", "intnetValue: " + intnetValue));
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        registerReceiver(this.mBCR_VALUE_SENT, new IntentFilter(MyIntentService.INTENT_ACTION));

        this.mbtnSend = (Button) findViewById(R.id.btn_send);
        this.mbtnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), MyIntentService.class);
                intent.putExtra("intent_key", ++i);
                startService(intent);
            }
        });
    }
}

MyIntentService:

public class MyIntentService extends IntentService {

private final String TAG = this.getClass().getSimpleName();
public final static String INTENT_ACTION = "ACTION_VALUE_SENT";
public final static String INTENT_KEY = "INTENT_KEY";

public MyIntentService() {
    super(null);
}

/**
 * Creates an IntentService.  Invoked by your subclass's constructor.
 *
 * @param name Used to name the worker thread, important only for debugging.
 */
public MyIntentService(String name) {
    super(name);
    setIntentRedelivery(true);
}

@Override
public void onCreate() {
    super.onCreate();
    Log.w(TAG, SubTag.msg("onCreate"));
}

@Override
protected void onHandleIntent(Intent intent) {
    Log.w(TAG, SubTag.msg("onHandleIntent"));

    int intent_value = intent.getIntExtra("intent_key", -1);
    Log.i(TAG, SubTag.bullet("", "intent_value: " + intent_value));

    Intent intent2 = new Intent();
    intent2.setAction(MyIntentService.INTENT_ACTION);
    intent2.putExtra(MyIntentService.INTENT_KEY, intent_value);
    sendBroadcast(intent2);

    SystemClock.sleep(3000);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.w(TAG, SubTag.msg("onStartCommand"));

    return super.onStartCommand(intent, flags, startId);
}
mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • Main difference that `Service` is working on the same thread where it was called. And `IntentService` working on a background thread – xAqweRx Jul 18 '16 at 11:08

2 Answers2

0

In short, a Service is a broader implementation for the developer to set up background operations, while an IntentService is useful for "fire and forget" operations, taking care of background Thread creation and cleanup.

From the docs:

Service A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.

IntentService Service is a base class for IntentService Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

Refer this doc - http://developer.android.com/reference/android/app/IntentService.html

0

Service

This is the base class for all services. When you extend this class, it’s important that you create a new thread in which to do all the service’s work, because the service uses your application’s main thread, by default, which could slow the performance of any activity your application is running.

IntentService

This is a subclass of Service that uses a worker thread to handle all start requests, one at a time. This is the best option if you don’t require that your service handle multiple requests simultaneously. All you need to do is implement onHandleIntent(), which receives the intent for each start request so you can do the background work.

Below are some key differences between Service and IntentService in Android.

1) When to use?

The Service can be used in tasks with no UI, but shouldn’t be too long. If you need to perform long tasks, you must use threads within Service.

The IntentService can be used in long tasks usually with no communication to Main Thread. If communication is required, can use Main Thread handler or broadcast intents. Another case of use is when callbacks are needed (Intent triggered tasks).

2) How to trigger?

The Service is triggered calling to method onStartService().

The IntentService is triggered using an Intent, it spawns a new worker thread and the method onHandleIntent() is called on this thread.

for more clarity refer this http://www.onsandroid.com/2011/12/difference-between-android.html

Uma Achanta
  • 3,669
  • 4
  • 22
  • 49
  • in the point number 1, you stated that in the Service one cant manipulate the UI thread...is the possible in Intentservice?? – Amrmsmb Jul 18 '16 at 12:31
  • both have no communication with UI. but if required in intent service we have to use Main Thread handler or broadcast intents. – Uma Achanta Jul 18 '16 at 12:38
  • I can see lots of similarities in your answer to [this one](https://stackoverflow.com/a/15772151/1364007). Could you use the quote highlighting (by starting a line with a `>`) to show which lines are your words, and which lines were copy/pasted? That will prevent others from possibly accusing you from plagiarism. – Wai Ha Lee Jul 18 '16 at 13:26