android check if user turned off location

旧街凉风 提交于 2019-11-29 16:02:47

You can use location state changed receiver to know when user manually turns of location GpsReceiver.java

public class GpsReceiver extends BroadcastReceiver {
    private final LocationCallBack locationCallBack;

    /**
     * initializes receiver with callback
     * @param iLocationCallBack Location callback
     */
    public GpsReceiver(LocationCallBack iLocationCallBack){
            this.locationCallBack = iLocationCallBack;
    }

    /**
     * triggers on receiving external broadcast
     * @param context Context
     * @param intent Intent
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
            locationCallBack.onLocationTriggered();
        }
    }
}

Create an interface to communicate GPS changes to Activity

public interface LocationCallBack {
    /**
     * on Location switch triggered
     */
    void onLocationTriggered();
}

Register receiver in onCreate() of Activity to start listening to GPS state changes

public void onCreate(Bundle savedInstance){
//---
registerReceiver(new GpsReceiver(new LocationCallBack() {
            @Override
            public void onLocationTriggered() {
                //Location state changed
            }
        }), new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
//---
}
Niraj Sanghani

You can use below to check Location availability:

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    try {
        gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }catch (Exception ex){}
    try{
        network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }catch (Exception ex){}
    if(!gps_enabled && !network_enabled){
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setMessage(getResources().getString(R.string.gps_network_not_enabled));
        dialog.setPositiveButton(getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {                 
                Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                Startup.this.startActivity(myIntent);                    
            }
        });
        dialog.setNegativeButton(getString(R.string.Cancel), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub

            }
        });
        dialog.show();
    }

This code worked on Android 9 (Pie) version perfectly

MainActivity

public class MainActivity extends AppCompatActivity {

    private MyLocationReceiver mLocationReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLocationReceiver = new MyLocationReceiver(this, Snackbar.make(findViewById(android.R.id.content), "Location service is not enabled", Snackbar.LENGTH_INDEFINITE));

    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mLocationReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
    }

    @Override
    protected void onPause() {
        super.onPause();
        try {
            unregisterReceiver(mLocationReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

MyLocationReceiver

public class MyLocationReceiver extends BroadcastReceiver {

    private static final String TAG = "MyLocationReceiver";
    private Context context;
    private Snackbar snackbar;

    public MyLocationReceiver(Context context, Snackbar snackbar){
        this.context = context;
        this.snackbar = snackbar;
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
            boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            boolean networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            if(gpsEnabled && networkEnabled) {
                if (snackbar != null) {
                    snackbar.dismiss();
                }
                Log.d(TAG, "GPS is enabled");
            } else {
                snackbar.show();
                Log.d(TAG, "GPS is disabled");
            }
        }

    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!