how to start service from fragments

Swap-IOS-Android picture Swap-IOS-Android · Oct 22, 2012 · Viewed 39.5k times · Source

I want to start service from fragment from a list view item. I am trying to call service with:

startService(new Intent(getActivity(),myPlayService.class));

But it wont work at all. How do i call my service from fragments? Is there any another way to start service?

Answer

DroidBender picture DroidBender · Oct 22, 2012

Replace

startService(new Intent(getActivity(),myPlayService.class));

with

getActivity().startService(new Intent(getActivity(),myPlayService.class));