getSystemServices is undefined when called in a Fragment?

AndroidAndroid FragmentsAndroid Sensors

Android Problem Overview


I want TextViews to display the sensors readings in a Fragment. When trying to initialize the SensorManager the getSystemServices is undefined in the Fragment, eclipse says.Why and how to fix it.

Fragment

public class FragSensors extends Fragment {

private TextView accXTv, accYTv, accZTv;
private SensorManager sensorManager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
		Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	View v = inflater.inflate(R.layout.frag_sensors, container, false);
	accXTv = (TextView) v.findViewById(R.id.accXValue);
	accYTv = (TextView) v.findViewById(R.id.accYValue);
	accZTv = (TextView) v.findViewById(R.id.accZValue);
	return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onActivityCreated(savedInstanceState);
	sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
	
}

private final SensorEventListener mSensorListener = new SensorEventListener() {
	
	@Override
	public void onSensorChanged(SensorEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}
};

}

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

Use:

getActivity().getSystemService(name)

Solution 3 - Android

On Kotlin use:

this.sensorManager = activity!!.getSystemService(Context.SENSOR_SERVICE) as SensorManager

Solution 4 - Android

sensorManager = (SensorManager) getActivity().getSystemService(Context.NAMEOF_SERVICE);

Fragments cannot directly call System Services , You have to use Activity with which these Fragment is Attached

Solution 5 - Android

sensorManager = (SensorManager) 
requireActivity().getSystemService(Context.SENSOR_SERVICE);

Solution 6 - Android

In my case this helped:

val manager = context!!.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

Solution 7 - Android

You can also get the context from the LayoutInflater.

SensorManager sm = (SensorManager) getLayoutInflater().getContext().getSystemService(Context.SENSOR_SERVICE);

Solution 8 - Android

For kotlin this is what worked for me.

private fun hideKeyboard() {
        val inputManager = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        if (inputManager.isAcceptingText) {
            inputManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, 0)
        }
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2121View Question on Stackoverflow
Solution 1 - AndroidAn SO UserView Answer on Stackoverflow
Solution 2 - AndroidZoranView Answer on Stackoverflow
Solution 3 - AndroidgundraburView Answer on Stackoverflow
Solution 4 - AndroidMuhammad UsmanView Answer on Stackoverflow
Solution 5 - AndroidAnikView Answer on Stackoverflow
Solution 6 - Androiduser1892777View Answer on Stackoverflow
Solution 7 - AndroidJeffreyView Answer on Stackoverflow
Solution 8 - AndroidthesamoanppprogrammerView Answer on Stackoverflow