webyonet / react-native-android-location-services-dialog-box

React Native Android Location Services Dialog Box
MIT License
182 stars 50 forks source link

locationProviderStatusChange listener callback method calling multiple times when closing (on/off) location from notification tray. #58

Closed penmatsa closed 6 years ago

penmatsa commented 6 years ago

constructor(){ super(); this.DeviceEventEmitterListener = this.DeviceEventEmitterListener.bind(this); }

componentWillMount(){

  DeviceEventEmitter.addListener('locationProviderStatusChange',this.DeviceEventEmitterListener);

this.enableLocations(); }

componentWillUnmount() {

  LocationServicesDialogBox.stopListener();

}

DeviceEventEmitterListener=(params) => {

  alert(params);  // ISSUE:  alert showing multiple times when the user closes app multiple times.

}

enableLocations(){

LocationServicesDialogBox.checkLocationServicesIsEnabled({
  message: "<h2>Use Location ?</h2>This app wants to change your device settings:<br/><br/>Use 
 GPS, Wi-Fi, and cell network for location<br/><br/>",
  ok: "YES",
  cancel: "NO",
  enableHighAccuracy: true, // true => GPS AND NETWORK PROVIDER, false => GPS OR NETWORK PROVIDER
  showDialog: true, // false => Opens the Location access page directly
  openLocationServices: true, // false => Directly catch method is called if location services are turned off
  preventOutSideTouch: false, // true => To prevent the location services window from closing when it is clicked outside
  preventBackClick: true, // true => To prevent the location services popup from closing when it is clicked back button
  providerListener: true // true ==> Trigger locationProviderStatusChange listener when the location state changes

}).then((success) =>{ console.log(success); // success => {alreadyEnabled: false, enabled: true, status: "enabled"}

  if(success.enabled){
    this.setState({
      locationEnabled: true
          });
          this.getPermission();  // get android app level permissions
  }else{

    this.setState({
      locationEnabled: false
          }); 
  }  

}).catch((error) => {

  // if user open settings screen and comes back without enabling location  reopen dialog or user 
 //  clicks no in location enable dialog
  this.enableLocations();

}); }

webyonet commented 6 years ago

this is the right way of working. if you do not want to be triggered again when the GPS provider is turned off, you can stop the listener with the LocationServicesDialogBox.stopListener() method.

DeviceEventEmitterListener=(params) => {
  if(!params. enabled){
     LocationServicesDialogBox.stopListener();
  }
}
penmatsa commented 6 years ago

Hi,

if I off location for once. the listener is calling multiple times and so It enters if condition multiple times.

webyonet commented 6 years ago

android version? react native version ? what type of device are you using ? (emulator or real)

penmatsa commented 6 years ago

android version : 26(SDK) react native version : 0.56.0 device : Samsung s8 (oreo)

zmnv commented 4 years ago

The same situation. Any solutions?

zmnv commented 4 years ago

It's not a problem of react-native-android-location-services-dialog-box. It's a problem about touchable events on Android itself.

You should use deboune like:

import { debounce } from 'lodash';

...

DeviceEventEmitter.addListener('locationProviderStatusChange', debounce(this.DeviceEventEmitterListener));