how to check if location is enabled android

Solutions on MaxInterview for how to check if location is enabled android by the best coders in the world

showing results for - "how to check if location is enabled android"
Jessica
24 Feb 2019
1LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
2boolean gps_enabled = false;
3boolean network_enabled = false;
4
5try {
6    gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
7} catch(Exception ex) {}
8
9try {
10    network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
11} catch(Exception ex) {}
12
13if(!gps_enabled && !network_enabled) {
14    // notify user
15    new AlertDialog.Builder(context)
16        .setMessage(R.string.gps_network_not_enabled)
17        .setPositiveButton(R.string.open_location_settings, new DialogInterface.OnClickListener() {
18            @Override
19            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
20                context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
21            }
22        }
23        .setNegativeButton(R.string.Cancel,null)
24        .show();    
25}