getSystemServices is undefined when called in a Fragment?
Use:
getActivity().getSystemService(name)
Just one more method call:
sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
Why that one extra method call?
the getSystemService()
method that provides access to system services comes from Context
. An Activity
extends Context
, a Fragment
does not. Hence, you first need to get a reference to the Activity
in which the Fragment
is contained and then magically retrieve the system service you want.
On Kotlin use:
this.sensorManager = activity!!.getSystemService(Context.SENSOR_SERVICE) as SensorManager