Search code examples
androidandroid-activityandroid-service

Start a Service from an Activity


In my app, I have an Activity from which I want to start a Service. Can anybody help me?


Solution

  • The application can start the service with the help of the Context.startService method. The method will call the onCreate method of the service if service is not already created; else onStart method will be called. Here is the code:

    Intent serviceIntent = new Intent();
    serviceIntent.setAction("com.testApp.service.MY_SERVICE");
    startService(serviceIntent);