java - 使用 android 获取当前位置的代码中出现奇怪的错误

标签 java android android-manifest android-permissions

我创建了自己的 Java 类来处理位置:

public class MyLocationHandler extends Service implements LocationListener {

......

 public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                // First get location from Network Provider
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

.......

}

这是 build.gradle 的摘录:

  android {
        compileSdkVersion 23
        buildToolsVersion "24.0.0"

        defaultConfig {
            applicationId "com.mobile.appoperator"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }

问题: 问题是我需要添加 checkSelfPermission,但这不是一个 Activity ,而且如果我添加它,我就无法在 api lvl 23 之前的 Android 版本上运行我的应用程序。

那么我该如何解决这个问题呢?

最佳答案

我正在服务中执行我的操作,是的,我目前不请求许可,因为出于某种原因,您必须在 Activity 中才能执行此操作;

这是我所做的:

private void updateUserLocation(){
    if ( Build.VERSION.SDK_INT >= 23 &&
            ContextCompat.checkSelfPermission( getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION )
                    != PackageManager.PERMISSION_GRANTED &&
            ContextCompat.checkSelfPermission( getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
        return;
    }

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

    if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){

        LocationListener listener = new GpsListener();

        Looper.prepare();
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
        Looper.loop();

    }else {
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

            LocationListener listener = new GpsListener();

            Looper.prepare();
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
            Looper.loop();
        }
    }
}

实际请求用户授予您权限的最佳方法是尽早执行此操作,也许是在应用程序的第一个 Activity (例如“登录”)中执行此操作(然后他们会看到一些窗口,解释为什么您需要在应用程序中启用 GPS 或此类权限。

如果用户拒绝许可请求,那么您将不得不重新路由您的应用 - 基本上会感到悲伤并执行没有位置功能时会执行的操作。

摘要

一般来说,由于您只要求用户授予权限也许一次,因此您可以在他们登录时执行此操作(因为在授予权限后,就不会再次询问他们),然后在实际获取位置更新时,您可以像我下面一样简单地检查并继续!

我希望这可以帮助你有所收获。

关于java - 使用 android 获取当前位置的代码中出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38295484/

相关文章:

android - 三星s4 zoom不支持平滑缩放isSmoothZoomSupported()==false

android - 应用发布到市场后与所有设备不兼容

java - JDK 8 在类路径中不可用

java - 错误的资源泄漏警告?

java - Spring任务执行器安排了太多的任务实例

java - list 合并失败错误

android-manifest - 如何创建 Android Studio 构建脚本以在构建版本中插入 git commit hash?

仅使用一个扫描仪的 java.util.NoSuchElementException

android - 横向模式下 10 英寸设备上的偏好 Activity

android - RadioButton 在取消选中后留下黑色选中的圆圈