Detecting GPS on/off switch in Android phones

后端 未结 4 886
心在旅途
心在旅途 2020-11-30 01:20

I would like to detect when the user changes the GPS settings on or off for an Android phone. Meaning when user switches GPS sattelite on/off or detection via access points

相关标签:
4条回答
  • 2020-11-30 01:41

    Here is a code sample for a BroadcastReceiver detecting GPS location ON/OFF events:

    private BroadcastReceiver locationSwitchStateReceiver = new BroadcastReceiver() {
    
            @Override
            public void onReceive(Context context, Intent intent) {
    
                if (LocationManager.PROVIDERS_CHANGED_ACTION.equals(intent.getAction())) {
    
                    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                    boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                    boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
                    if (isGpsEnabled || isNetworkEnabled) {
                        //location is enabled
                    } else {
                        //location is disabled
                    }
                }
            }
        };
    

    Instead of changing your manifest file, you can register your BroadcastReceiver dynamically:

    IntentFilter filter = new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION);
    filter.addAction(Intent.ACTION_PROVIDER_CHANGED);
    mActivity.registerReceiver(locationSwitchStateReceiver, filter);
    

    Don't forget to unregister the receiver in your onPause() method:

    mActivity.unregisterReceiver(locationSwitchStateReceiver);
    
    0 讨论(0)
  • 2020-11-30 01:58

    Try this,

    try {
            locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    
            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    
                Log.i("About GPS", "GPS is Enabled in your devide");
            } else {
                //showAlert
            }
    
    0 讨论(0)
  • 2020-11-30 01:58

    Impement android.location.LocationListener, there you have two functions

    public void onProviderEnabled(String provider);
    public void onProviderDisabled(String provider);
    

    Using this you can find out when the requested provider is turned on or off

    0 讨论(0)
  • 2020-11-30 02:05

    As I have found out the best way to do this is to attach to the

    <action android:name="android.location.PROVIDERS_CHANGED" />
    

    intent.

    For instance:

    <receiver android:name=".gps.GpsLocationReceiver">
            <intent-filter>
                <action android:name="android.location.PROVIDERS_CHANGED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
    

    And then in the code:

    public class GpsLocationReceiver extends BroadcastReceiver implements LocationListener 
    ...
    
    @Override
    public void onReceive(Context context, Intent intent)
    {
            if (intent.getAction().matches("android.location.PROVIDERS_CHANGED"))
            { 
                // react on GPS provider change action 
            }
    }
    
    0 讨论(0)
提交回复
热议问题