Is it possible to get a notification when any location provider is enabled/disabled and ascertain what action occurred?

前端 未结 4 2197
挽巷
挽巷 2021-02-20 18:32

I wish to receive a notification when the user enables or disables either Network or GPS locations, and importantly I wish to know which one they have changed and how. I have a

4条回答
  •  轮回少年
    2021-02-20 19:22

    This works for me:

    Add receiver to the Manifest file:

    
            
                
            
        
    

    Check both location providers in receiver:

    public class LocationProviderChangedReceiver  extends BroadcastReceiver{
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            boolean anyLocationProv = false;
            LocationManager locationManager = (LocationManager) MyMainActivity.context.getSystemService(Context.LOCATION_SERVICE);
    
            anyLocationProv |= locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            anyLocationProv |=  locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
            Log.i("", "Location service status" + anyLocationProv);
    
    
        }
    
    }
    

    Though this receiver is called more than once due to obvious reasons, but this will tell you the status.

提交回复
热议问题