如何从片段开始服务

Swa*_*oid 27 android android-intent android-service

我想从列表视图项的片段开始服务.我试图用以下方式呼叫服务:

startService(new Intent(getActivity(),myPlayService.class));
Run Code Online (Sandbox Code Playgroud)

但它根本不起作用.我如何从片段中调用我的服务?还有其他方法可以开始服务吗?

Dro*_*der 97

更换

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

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

  • 但如果他不使用getActivity()它甚至都不会编译 (2认同)
  • @IgorGanapolsky,这就是原帖所说的.他们在问如何编译. (2认同)