Hey-men-whatsup Hey-men-whatsup - 2 months ago 14
Android Question

in onStartCommand() "this" refer to the service or application context?

I'm doing :

Toast.makeText(this,"Start Service",Toast.LENGTH_SHORT).show();


In this block (Service's method)

@Override
public int onStartCommand(){
...
}


What confuses me is that , why can I run
Toast
using Service's context, shouldn't toast appear on activity itself?

Answer

in onStartCommand() “this” refer to the service or application context?

In Java, this always refers to the instance of the object in whose class' method your code is running. So, this in onStartCommand() of a Service will be the Service.

shouldn't toast appear on activity itself?

A Toast does not require an activity.

Comments