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

Strange error into code to get current location with android

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

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,而且如果我添加它,我的应用程序将无法在 Android 版本上 运行早于 api 23 级。

那么我该如何处理这个问题呢?

我在一个服务中做我的,是的,我目前不请求许可,因为出于某种原因,你必须在 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 尽早执行此操作,例如登录(然后他们会看到一些 window 解释您为什么需要在您的应用中启用 GPS 或此类权限。

如果用户拒绝许可请求,那么您将不得不重新路由您的应用程序 - 基本上会感到难过并做没有位置功能时您会做的事情。

摘要

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

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