Start a Service from activity

MAkS picture MAkS · Feb 25, 2010 · Viewed 59.4k times · Source

In my app i have an activity from which i want to start an Service Can any body help me?

Answer

Vihaan Verma picture Vihaan Verma · Sep 13, 2012

Add this in your code

Intent serviceIntent = new Intent(this, ServiceName.class);
    startService(serviceIntent);

Dont forget to add service tag in AndroidManifest.xml file

<service android:name="com.example.ServiceName"></service>

From the Android official documentation:

Caution: A service runs in the same process as the application in which it is declared and in the main thread of that application, by default. So, if your service performs intensive or blocking operations while the user interacts with an activity from the same application, the service will slow down activity performance. To avoid impacting application performance, you should start a new thread inside the service.