在 Android 6.0.1 returns 0.0 中的位置,但在 4.0.1 中工作

Location in Android 6.0.1 returns 0.0, but works in 4.0.1

我已经搜索了此问题的解决方案并尝试了我能找到的相关解决方案,但仍然无法正常工作。代码的特定部分不是 运行。我遇到的问题是这一切都适用于我的旧 NEC Terrain(4.0.1,API 15),但不适用于我的新 Blackberry Priv(6.0.1,API 23) .在较新的 phone 中,第一个错误日志 "Net enabled" 出现在 logcat 中,然后 none 其他错误日志出现(我正在使用它们进行故障排除) .在旧的 phone 中,一切正常,所有日志都显示出来。是什么阻止此代码来自 运行?我添加了用于添加权限的代码,我已在下面发布。我的清单中还有以下内容:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission-sdk-23 android:name="android.permission.INTERNET" />
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION"/>

这是 locationManager 的声明:

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

代码如下:

if (isNetworkEnabled) {
                Log.e("Tag", "Net Enabled");

                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    Log.e("Tag", "No permission");
                }
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                if (locationManager == null) {
                    Log.e("Tag", "It's null.");
                }
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    Log.e("Tag", "NetLocMan OK");

                    if (location != null) {
                        Log.e("Tag", "NetLoc OK");
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        Log.e("Tag", "" + latitude);
                        Log.e("Tag", "" + longitude);
                    }
                }

            }

            if (isGPSEnabled) {
                Log.e("Tag", "Location" + location);
                if (location == null) {
                    Log.e("Tag", "GPSLoc finding");
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.e("Tag", "Location" + location);

                    if (locationManager != null) {
                        Log.e("Tag", "GPSLocMan OK");
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        Log.e("Tag", "Location" + location);

                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                            Log.e("Tag", "" + latitude);
                            Log.e("Tag", "" + longitude);
                        }
                    }
                }
            }

这是我正在使用的权限代码:

private static final int PERMS_REQUEST_CODE = 123;

if (!hasPermissions()){
            requestPerms();
        }

private boolean hasPermissions(){
        int res = 0;
        //string array of permissions,
        String[] permissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};

        for (String perms : permissions){
            res = checkCallingOrSelfPermission(perms);
            if (!(res == PackageManager.PERMISSION_GRANTED)){
                return false;
            }
        }
        return true;
    }

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        boolean allowed = true;

        switch (requestCode){
            case PERMS_REQUEST_CODE:

                for (int res : grantResults){
                    // if user granted all permissions.
                    allowed = allowed && (res == PackageManager.PERMISSION_GRANTED);
                }

                break;
            default:
                // if user not granted permissions.
                allowed = false;
                break;
        }

        if (allowed){
            //user granted all permissions we can perform our task.

        }
        else {
            // we will give warning to user that they haven't granted permissions.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)){
                    Toast.makeText(this, "Permissions denied.\nCannot continue.", Toast.LENGTH_SHORT).show();
                }
                if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION)){
                    Toast.makeText(this, "Permissions denied.\nCannot continue.", Toast.LENGTH_SHORT).show();
                }
            }
        }

    }

感谢您的帮助!如果您需要更多代码,请告诉我。

从 Android 6.0 开始(API 级别 23)Google 要求开发人员在 运行 时间请求潜在危险的权限:

ActivityCompat.requestPermissions(MainMenu.this, 
        Manifest.permission.ACCESS_FINE_LOCATION, 
        GET_PERMISSIONS_REQUEST_CODE
);

这显示 pop-up 请求访问用户位置的权限。您之前提出的任何位置请求都将 return 清空。