【问题标题】:Show Popup when Location access is disable by user (Andorid Google Maps)用户禁用位置访问时显示弹出窗口(Android Google 地图)
【发布时间】:2014-06-11 10:21:51
【问题描述】:

我如何显示弹出窗口以启用您的位置访问并打开位置访问意图。现在我正在创建包含地图的应用程序。我已启用 (map.setMyLocationEnabled(true))。并且应用程序显示当前位置按钮,但是当位置访问被禁用时,它不会显示任何响应。 我想要如果位置访问被禁用弹出显示用户启用位置访问

提前致谢

【问题讨论】:

  • 只需检查 n/w 或 gps 的位置管理器,如果它是真的离开它,否则会显示弹出窗口

标签: android google-maps maps


【解决方案1】:

此代码虽然可以满足您的所有需求,但是否有任何类型的位置服务可用?、提供者,以及如果不是位置管理器提供者的错误消息

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();
    }

}

如何使用这个

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);
}

}

【讨论】:

  • 谢谢。太糟糕了,您使用的是非标准名称。 LocationManager_check ?为什么不只是LocationManagerCheckyour_activity?此外,Java 有枚举。当枚举非常适合您的用例时,不要使用整数。
【解决方案2】:

您可以在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();
    }

并在您的活动中添加以下方法:

/**
 * 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】:

    您可以将 AlertDialog 用于弹出窗口。

            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/

    【讨论】:

      猜你喜欢
      • 2019-11-16
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多