如何在fragment里启动service
参考资料:http://stackoverflow.com/questions/13007355/how-to-start-service-from-fragments
问题:
如果在fragment里直接用startService,那么编译将不会通过。
如下代码在activity里是可以正确使用的,但是在fragment里却不能。
startService(new Intent(getActivity(),myPlayService.class));
解决办法:
替换
startService(new Intent(getActivity(),myPlayService.class));
为:
getActivity().startService(new Intent(getActivity(),myPlayService.class));