java - 当用户禁用 GPS 时如何在 MapActivity 上收到警报

标签 java android google-maps-api-3

我正在使用java在android中制作一个应用程序,它在 map 上显示当前地址,并在gps关闭时发出警报。

public class LocationManagerCheck {

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

    public LocationManagerCheck(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 activity) {

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

        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);
                                activity.startActivity(intent);
                                dialog.dismiss();
                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.dismiss();
                            }
                        });
        alert = builder.create();
        alert.show();
    }

我使用的代码仅在 Mapactivity 启动时显示警报,但在 activity 期间不显示警报。我只想在 GPS 关闭时向我显示警报 bo或者在 MapActivity 打开期间由用户禁用,请帮助我找到正确的代码,提前致谢。

最佳答案

您可能想知道用户是否打开/关闭 GPS。这个question可能有帮助

关于java - 当用户禁用 GPS 时如何在 MapActivity 上收到警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58284517/

相关文章:

java - 使用点分隔字符串访问(私有(private))成员

android - AlarmManager 不重复

c# - 使用 Web 服务将数据从 Android 应用程序插入到 SQL 服务器数据库

javascript - 谷歌地图 - 为多个标记重用单个信息窗口

google-maps-api-3 - Google.maps panTo

javascript - Google map 在 LatLng.toUrlValue 中返回经度 >180

java - Fiddler 不捕获来自 Java 进程的网络调用?

Java JFrame setSize 无法正常工作

java - 背压时 Intent 未清除, "on create"中的项目不断堆叠。安卓

android - android lollipop字体 "HelveticaNeue.ttf"偏差