Show Popup when Location access is disable by user (Andorid Google Maps)

亡梦爱人 提交于 2019-12-08 00:57:09

问题


how can i show popup which as enable your location access and open Location access intent. right now i am creating app that contain map. i have enabled (map.setMyLocationEnabled(true)). and the app show the current location button but when location access is disable it doesnt show any response. I want if the location access is disable a popup show which says user to enable the location access

thanks in advance


回答1:


this code while give you all you want, is any kind of location service available or not ?, Provider, and error message if the is not location manager provider

public class LocationManager_check {

    LocationManager locationManager;
    Boolean locationServiceBoolean = false;
    int providerType = 0;
    static AlertDialog alert;

    public LocationManager_check(Context context) {
        locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        boolean gpsIsEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean networkIsEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (networkIsEnabled == true && gpsIsEnabled == true) {
            locationServiceBoolean = true;
            providerType = 1;

        } else if (networkIsEnabled != true && gpsIsEnabled == true) {
            locationServiceBoolean = true;
            providerType = 2;

        } else if (networkIsEnabled == true && gpsIsEnabled != true) {
            locationServiceBoolean = true;
            providerType = 1;
        }

    }

    public Boolean isLocationServiceAvailable() {
        return locationServiceBoolean;
    }

    public int getProviderType() {
        return providerType;
    }

    public void createLocationServiceError(final Activity activityObj) {

        // show alert dialog if Internet is not connected
        AlertDialog.Builder builder = new AlertDialog.Builder(activityObj);

        builder.setMessage(
                "You need to activate location service to use this feature. Please turn on network or GPS mode in location settings")
                .setTitle("LostyFound")
                .setCancelable(false)
                .setPositiveButton("Settings",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Intent intent = new Intent(
                                        Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                activityObj.startActivity(intent);
                                dialog.dismiss();
                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.dismiss();
                            }
                        });
        alert = builder.create();
        alert.show();
    }

}

How to use this

LocationManager_check locationManagerCheck = new LocationManager_check(
                    this);
            Location location = null;

if(locationManagerCheck .isLocationServiceAvailable){

    if (locationManagerCheck.getProviderType() == 1)
                location = locationManager
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    else if (locationManagerCheck.getProviderType() == 2)
                location = locationManager
                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
}else{
    locationManagerCheck .createLocationServiceError(your_activity.this);
}

}




回答2:


You can add the following statements in onResume:

    // Make sure that GPS is enabled on the device
    LocationManager mlocManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    boolean enabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if(!enabled) {
       showDialogGPS();
    }

And add the following method in your activity:

/**
 * Show a dialog to the user requesting that GPS be enabled
 */
private void showDialogGPS() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(false);
    builder.setTitle("Enable GPS");
    builder.setMessage("Please enable GPS");
    builder.setInverseBackgroundForced(true);
    builder.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            startActivity(
                    new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        }
    });
    builder.setNegativeButton("Ignore", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}



回答3:


You can use the AlertDialog for popups.

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);

        // set title
        alertDialogBuilder.setTitle("Your Title");

        // set dialog message
        alertDialogBuilder
            .setMessage("Click yes to exit!")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked do some stuff
                }
              })
            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();

http://www.mkyong.com/android/android-alert-dialog-example/



来源:https://stackoverflow.com/questions/24160472/show-popup-when-location-access-is-disable-by-user-andorid-google-maps

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