57

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

    }
};

}

Amrmsmb
  • 1
  • 27
  • 104
  • 226

8 Answers8

187

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.

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • 2
    `getActivity()` can return null if it's not attached in the moment. Although it's extremely rare condition it can happen. – ruX Jun 28 '18 at 20:27
33

Use:

getActivity().getSystemService(name)
Zoran
  • 1,484
  • 1
  • 10
  • 13
6

On Kotlin use:

this.sensorManager = activity!!.getSystemService(Context.SENSOR_SERVICE) as SensorManager
gundrabur
  • 843
  • 10
  • 12
4
sensorManager = (SensorManager) getActivity().getSystemService(Context.NAMEOF_SERVICE);

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

Muhammad Usman
  • 795
  • 7
  • 19
4
sensorManager = (SensorManager) 
requireActivity().getSystemService(Context.SENSOR_SERVICE);
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Anik
  • 51
  • 4
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – β.εηοιτ.βε May 24 '20 at 21:16
1

In my case this helped:

val manager = context!!.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
user1892777
  • 131
  • 2
  • 7
0

You can also get the context from the LayoutInflater.

SensorManager sm = (SensorManager) getLayoutInflater().getContext().getSystemService(Context.SENSOR_SERVICE);
Jeffrey
  • 1,998
  • 1
  • 25
  • 22
0

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)
        }
    }
Mr. Disability
  • 799
  • 1
  • 10
  • 19