-2

I have this code:

@Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_head_sound, container, false);
            mButtonStartService=(ImageButton)rootView.findViewById(R.id.imageButton);
            mButtonStartService.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(intent!=null)
                    {
                        getActivity().stopService(intent);
                        intent=null;
                        Log.d("intent","null");

                    }
                    else
                    {
                        intent=new Intent(getActivity(),MyIntentService.class);
                        getActivity().startService(intent);
                    }


                }
            });
            return rootView;
        }

But the IntentService don't stop and Proximity sensor continues to send values in onSensorChanged().

David Wasser
  • 93,459
  • 16
  • 209
  • 274
user1608228
  • 249
  • 1
  • 4
  • 10

1 Answers1

0

At the end of your onSensorChanged() method you can call : sensorManager.unregisterListener(listener);

It is good practice as it will save the devices resources (battery for example).

Tleilaxus
  • 61
  • 3