如何在Fragment中获取对LocationManager的引用

mal*_*he4 15 android fragment locationmanager

我有一个扩展Fragment的类并实现了LocationListener.当我写作

LocationManager myLocalManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Run Code Online (Sandbox Code Playgroud)

我得到一个编译时错误,因为该方法getSystemService不是Fragment的方法.

我能做些什么来创造LocationManager

Raf*_*l T 62

你的片段里面只需要调用:

LocationManager mgr = 
(LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
Run Code Online (Sandbox Code Playgroud)

所以你只需getSystemService()从那里获得活动和电话