Android 6.0.1中的位置返回0.0,但适用于4.0.1

时间:2017-02-13 23:45:08

标签: android android-6.0-marshmallow locationmanager

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

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

    }

感谢您的帮助!如果您需要更多代码,请与我们联系。

1 个答案:

答案 0 :(得分:3)

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

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

这会显示一个弹出窗口,要求获得访问用户位置的权限。您之前提出的任何位置请求都将返回空白。