Search code examples
androidbroadcastreceiver

How to stop a broadcast receiver to receive action?


I can start a broadcast receiver by this code which detects my location update

Intent intent = new Intent(
                        LocationChangedReciever.ACTION_LOCATION_CHANGED);
                LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                long minTime = 1000; // This should probably be fairly long
                float minDistance = 1; // This should probably be fairly big
                String provider = "gps"; // GPS_PROVIDER or NETWORK_PROVIDER

                PendingIntent launchIntent = PendingIntent.getBroadcast(this,
                        0, intent, 0);
                manager.requestLocationUpdates(provider, minTime, minDistance,
                        launchIntent);

I did it in one my button click, but once I click that button the receiver starts to work for all the time, but i have a stop button which will disable the receiver, but I couldnt do it, this receiver works unless I uninstall it, any help?


Solution

  • Call removeUpdates(launchIntent). For this to work, you'll need to keep a reference to the intent stored for the remove call.